【发布时间】:2020-06-16 17:03:25
【问题描述】:
当我将时间序列日期分配为 datetimeindex 时,我的时间序列日期是混乱的(日/月)。奇怪的是解析器可能会出错,但尝试声明格式并使用 Dayfirst 但没有任何效果。
#input_data = pd.read_csv(url)
input_data = pd.read_csv(url,usecols=['Dates','TYAFWD Comdty'],skiprows=None, parse_dates=True, nrows=1500)
# Set Date as Index, clean dataframe
input_data = input_data.set_index('Dates')
df = pd.DataFrame(input_data).dropna()
print(df.columns)
# Create new Date index
data_time = pd.to_datetime(df.index)
datetime_index = pd.DatetimeIndex(data_time.values)
df = df.set_index(datetime_index)
df.index = pd.to_datetime(df.index, infer_datetime_format='%Y/%m/%d' )
df['year'] = pd.DatetimeIndex(df.index).year
df['month'] = pd.DatetimeIndex(df.index).month
df['week'] = pd.DatetimeIndex(df.index).weekofyear
print(df.head(30))
从输出中可以看出这一切都搞混了。 我希望输出中的所有条目都在 5 月,即第 5 个月,但它会将日期翻转一次
这是我的原始数据: https://raw.githubusercontent.com/esheehan1/projects/master/BB_FUT_DATA.csv
Index(['TYAFWD Comdty'], dtype='object')
TYAFWD Comdty year month week
2020-05-26 0.508 2020 5 22
2020-05-25 0.494 2020 5 22
2020-05-22 0.494 2020 5 21
2020-05-21 0.508 2020 5 21
2020-05-20 0.512 2020 5 21
2020-05-19 0.512 2020 5 21
2020-05-18 0.552 2020 5 21
2020-05-15 0.483 2020 5 20
2020-05-14 0.474 2020 5 20
2020-05-13 0.494 2020 5 20
2020-12-05 0.510 2020 12 49
2020-11-05 0.548 2020 11 45
2020-08-05 0.527 2020 8 32
2020-07-05 0.494 2020 7 27
2020-06-05 0.568 2020 6 23
2020-05-05 0.541 2020 5 19
【问题讨论】:
-
怎么了?你会期待什么结果?
-
抱歉不是很清楚。我希望输出中的所有条目都在第 5 个月的 5 月,但它会将日期翻转一次
-
能不能把你的数据贴出来,不看数据是无法回答的。
-
这里是 Github 上原始数据的路径。 raw.githubusercontent.com/esheehan1/projects/master/…
标签: pandas