【发布时间】:2020-05-26 18:13:46
【问题描述】:
我正在尝试使用 pandas 将我的数据框的索引转换为日期时间对象,但一直收到此错误--
“ValueError: 时间数据 'Jan 20' 与格式 '%b, %y' 不匹配(匹配)”。
我仔细检查了日期时间格式并删除了连字符,但仍然没有运气。我怎样才能解决这个问题?
这是我尝试过的:
import pandas as pd
table = pd.read_html('https://www.finra.org/investors/learn-to-invest/advanced-investing/margin-statistics')
#set the index to date column
df = table[0].set_index('Month/Year')
df.index = df.index.str.replace("-", " ")
df.index = pd.to_datetime(df.index, format="%b, %y")
【问题讨论】:
标签: python pandas dataframe datetime