How to convert panda series index date into DatetimeIndex? - python

The following data below is from a pandas series, but I need the date converted to DatetimeIndex like this format: 2020-08-17. The index of this series should be a pd.DatetimeIndex. What are some ways to convert it as such?
8/17/20 14082780.0
8/18/20 14277100.0
8/19/20 14483216.0
8/20/20 14685442.0
8/21/20 14886403.0
Length: 212, dtype: float64

Just change the index to be as type of datetime:
df.index = pd.to_datetime(df.index)
More generally for a non-index column:
df['Date']= pd.to_datetime(df['Date'])

Related

Converting dates to datetime64 results in day and month places getting swapped

I am pulling a time series from a csv file which has dates in "mm/dd/yyyy" format
df = pd.read_csv(lib_file.csv)
df['Date'] = df['Date'].apply(lambda x:datetime.strptime(x,'%m/%d/%Y').strftime('%d/%m/%Y'))
below is the output
I convert dtypes for ['Date'] from object to datetime64
df['Date'] = pd.to_datetime(df['Date'])
but that changes my dates as well
how do I fix it?
Try this:
df['Date'] = pd.to_datetime(df['Date'], infer_datetime_format=True)
This will infer your dates based on the first non-NaN element which is being correctly parsed in your case and will not infer the format for each and every row of the dataframe.
just using the below code helped
df = pd.read_csv(lib_file.csv)
df['Date'] = pd.to_datetime(df['Date])

Remove time from pandas dataframe datetime64[ns] index

I am trying to merge two pandas dataframes, and to do this I want to make it so that they both have the same index. The problem is, one df has an index of datatype object which just includes the date while the other df has an index of datatype datetime64[ns] which includes the date and time. Is there a way to make these both the same data type so that I can merge the two dataframes?
Convert both date types into a pandas datetime format and convert them with having just dates.
df['date_only'] = df['dates'].dt.date
You could convert a date and time format to just date as below
import pandas as pd
date_n_time='2015-01-08 22:44:09'
date=pd.to_datetime(date_n_time).date()
make your index as a column using
df.reset_index()
set it back to index using
df.set_index()

Pandas Dataframe asFreq changing datatype of index

I'm having an issue when using asfreq to resample a dataframe. My dataframe, df, has an index of type Datetime.Date(). After using df.asfreq('d','pad'), my dataframe index has been changed to type pandas.tslib.Timestamp. I've tried the following to change it back but I'm having no luck...
df = df.set_index(df.index.to_datetime())
df.index = df.index.to_datetime()
df.index = pd.to_datetime(df.index)
Any thoughts?
Thanks!
use pd.to_datetime
df.index = pd.to_datetime(df.index)
This is the canonical approach to creating datetime indices. If you want your index indices to all be of type datetime.datetime then you can do this following.
df.index = pd.Index([i.to_datetime() for i in df.index], name=df.index.name, dtype=object)
I just don't know why you'd want to.
Why is this a problem? If you really need a datetime.date you can try df.index = df.index.map(lambda x: x.date() since pandas.TimeStamp subclasses datetime.datetime

Python/Pandas - Convert type from pandas period to string

I have a DataFrame:
Seasonal
Date
2014-12 -1.089744
2015-01 -0.283654
2015-02 0.158974
2015-03 0.461538
I used a pd.to_period in the DataFrame, so its index has turned into a Pandas period type (type 'pandas._period.Period').
Now, I want to turn that index to strings. I'm trying to apply the following:
df.index=df.index.astype(str)
However that doesn't work...
ValueError: Cannot cast PeriodIndex to dtype |S0
My code has been frozen since then.
S.O.S.
You can use to_series and then convert to string:
print df
# Seasonal
#Date
#2014-12 -1.089744
#2015-01 -0.283654
#2015-02 0.158974
#2015-03 0.461538
print df.index
#PeriodIndex(['2014-12', '2015-01', '2015-02', '2015-03'],
# dtype='int64', name=u'Date', freq='M')
df.index=df.index.to_series().astype(str)
print df
# Seasonal
#Date
#2014-12 -1.089744
#2015-01 -0.283654
#2015-02 0.158974
#2015-03 0.461538
print df.index
#Index([u'2014-12', u'2015-01', u'2015-02', u'2015-03'], dtype='object', name=u'Date')
The line below should convert your PeriodIndex to string format:
df.index = df.index.strftime('%Y-%m')
You can convert the items to strings by specifying basestring:
df.index = df.index.astype(basestring)
or if that doesn't work:
df.index = df.index.map(str)
Refering to the comments from this answer, it might have to do with your pandas/python version.
If your index is a PeriodIndex, you can convert it to a str list as the following example shows:
import pandas as pd
pi = pd.period_range("2000-01", "2000-12", freq = "M")
print(list(pi.astype(str)))

String date to date (pandas)

I have a dataframe that is called dfactual this dataframe has a column ForeCastEndDate, so
dfactual['ForeCastEndDate'] it contains:
311205
311205
This must be a date in the format 31-12-2005, but the current format is int64. I tried the following:
dfactual['ForeCastEndDate'] = pd.to_datetime(pd.Series(dfactual['ForecastEndDate']))
I tried also to add the format command to it, but it didn't work out the format stays the same, int64.
How should I do it?
You can't use to_datetime with dtypes that are not str so you need to convert the dtype using astype first and then you can use to_datetime and pass the format string:
In [154]:
df = pd.DataFrame({'ForecastEndDate':[311205]})
pd.to_datetime(df['ForecastEndDate'].astype(str), format='%d%m%y')
Out[154]:
0 2005-12-31
Name: ForecastEndDate, dtype: datetime64[ns]

Categories