pottolom pottolom - 1 year ago 234
Python Question

Python/Pandas change date format for date_range index

I am running a script (script 1) to create an empty data frame which is populated by another script (script 2). The index in this empty data frame is a time series of 30 minute intervals across 365 days, beginning 1st October 2016. To create this time series index, Script 1 contains the following code:

time_period_start = dt.datetime(2016,01,10).strftime("%Y-%d-%m")
index = pd.date_range(time_period_start, freq='30min', periods=17520)

Script 2 pulls data out of a CSV file, containing values across a time series. The plan is for this script to put this data into a dataframe, and then merge this with the dataframe created in Script 1.

The problem I am having is that the format of the dates in the dataframe created in Script 2 is Y-D-M, which is what comes out of the CSV files. However, the format of the dates in the dataframe created in Script 1 is Y-M-D, which causes incorrect results when I try to merge. This is even despite my use of ".strftime("%Y-%d-%m")" in the first line of code above. Is there any way of amending the second line of code so that the output dataframe is in Y-D-M?

Answer Source

.strftime() isn't affecting the final dataframe, since pd.date_range transforms it back into a datetime anyway. Instead of trying to match on strings, you should convert the dates in the second dataframe (the one created by Script 2) to datetime as well.

df2.date = pd.to_datetime(df2.date, format='%Y-%d-%m')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download