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:) !
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.