'Error with a python string to dateconversion using Pandas
I'm struggling to change the format of the dates of my dataframe. I get the following error:
ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing
That's my code,
price_spx = spx.history(start="2010-01-01", end="2019-07-16")
components = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
spx_tickers = components[0]
spx_changes = components[1]
pd.to_datetime(spx_changes['Date'],format='%B %d, %Y')
Thanks for your help :) !
Solution 1:[1]
If you look carefully at what the spx_changes
looks like, you can see you have a multi-level column index (column names are tuples):
spx_changes.info()
#<class 'pandas.core.frame.DataFrame'>
#RangeIndex: 297 entries, 0 to 296
#Data columns (total 6 columns):
# # Column Non-Null Count Dtype
#--- ------ -------------- -----
# 0 (Date, Date) 297 non-null object
# 1 (Added, Ticker) 291 non-null object
# 2 (Added, Security) 291 non-null object
# 3 (Removed, Ticker) 285 non-null object
# 4 (Removed, Security) 285 non-null object
# 5 (Reason, Reason) 297 non-null object
#dtypes: object(6)
#memory usage: 14.0+ KB
Therefore, spx_changes['Date']
returns a DataFrame, because only one level is given. pd.to_datetime()
needs a Series of values, not a whole DataFrame (it is effectively as though you wrote pd.to_datetime(spx_changes, format="%B %d, %Y")
):
pd.to_datetime(spx_changes['Date', 'Date'], format='%B %D, %Y')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rawson |