PandasDataframe to_datetime error

Multi tool use


PandasDataframe to_datetime error
I have got the following indices on a dataframe:
data extracted Index(['2014-06-30 00:00:00.0', '2014-07-07 00:00:00.0',
'2014-08-11 00:00:00.0', '2014-08-18 00:00:00.0',
'2014-08-25 00:00:00.0', '2014-09-08 00:00:00.0',
'2014-09-22 00:00:00.0', '2014-09-29 00:00:00.0',
'2014-10-06 00:00:00.0', '2014-10-27 00:00:00.0',
'2014-11-24 00:00:00.0', '2014-12-15 00:00:00.0',
'2014-12-29 00:00:00.0', '2015-01-05 00:00:00.0',
'2015-01-19 00:00:00.0', '2015-01-26 00:00:00.0',
'2015-02-02 00:00:00.0', '2015-02-16 00:00:00.0',
'2015-02-23 00:00:00.0', '2015-04-13 00:00:00.0',
'2015-04-20 00:00:00.0', '2015-05-04 00:00:00.0',
'2015-05-25 00:00:00.0', '2015-06-01 00:00:00.0',
'2015-06-15 00:00:00.0', '2015-06-22 00:00:00.0',
'2015-06-29 00:00:00.0', '2015-07-13 00:00:00.0',
'2015-07-20 00:00:00.0', '2015-08-17 00:00:00.0',
'2015-08-24 00:00:00.0', '2015-08-31 00:00:00.0',
'2015-09-07 00:00:00.0', '2015-10-05 00:00:00.0',
'2015-10-12 00:00:00.0', '2015-10-19 00:00:00.0',
'2015-11-09 00:00:00.0', '2015-11-16 00:00:00.0',
'2015-11-30 00:00:00.0', '2016-01-18 00:00:00.0',
'2016-02-01 00:00:00.0', '2016-02-15 00:00:00.0',
'2016-02-29 00:00:00.0', '2016-03-14 00:00:00.0',
'2016-04-04 00:00:00.0', '2016-04-11 00:00:00.0',
'2016-04-25 00:00:00.0', '2016-05-16 00:00:00.0',
'2016-05-30 00:00:00.0', '2016-06-20 00:00:00.0',
'2016-06-27 00:00:00.0', '2016-07-18 00:00:00.0',
'2016-08-01 00:00:00.0', '2016-08-15 00:00:00.0',
'2016-08-22 00:00:00.0', '2016-09-12 00:00:00.0',
'2016-10-03 00:00:00.0', '2016-11-07 00:00:00.0',
'2016-11-14 00:00:00.0', '2016-11-21 00:00:00.0',
'2016-12-05 00:00:00.0', '2016-12-19 00:00:00.0', 'DATE'],
dtype='object', name='DATE')
I want to do a weekly resampling on monday on this dataframe indices, so i need to convert them to a datetime index:
data = pd.read_csv('statistic.csv',
parse_dates=True, index_col=['DATE'], low_memory=False)
data[['QUANTITY']] = data[['QUANTITY']].apply(pd.to_numeric, errors='coerce')
data_extracted = data.groupby(['DATE','ARTICLENO'])
['QUANTITY'].sum().unstack()
data_extracted = data_extracted.fillna(value=np.nan)
data_extracted.index = pd.to_datetime(data_extracted.index)
When i try to convert the index like above, i get an error:
ValueError: Unknown string format
I think it is the last entry ('DATE'). How can i remove this?data_extracted.index[:-1]
?
How to convert to a weekly series? I know the .resample('W-MON')
but read about some bugs and unexpected behaviour. Additionally, my data is not in regular distance, there is data every monday but not every seven days.
data_extracted.index[:-1]
.resample('W-MON')
1 Answer
1
You can use:
data_extracted.index = pd.to_datetime(data_extracted.index.str[:-2], errors='coerce')
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.