Pandas pd.to_datetime提供混乱的日期

Pandas pd.to_datetime提供混乱的日期,pandas,Pandas,当我将timeseries日期指定为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, c

当我将timeseries日期指定为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个月,但它会将日期翻转一次我看不出您的输出有任何错误

似乎是.to_datetime的默认行为。默认情况下,它按年、月、日的降序排列。这是标准

但是,如果要确保数据正确转换,请使用参数格式


如果可以的话,在编写代码对其进行操作之前,最好先查看一些原始数据

在您的特殊情况下,日期格式为D/M/Y,这是国际和欧洲标准。默认情况下,函数pd.read_csv使用美式日期格式M/D/Y

使用参数dayfirst=True更改此值将获得所需的输出。另外,我将您的代码缩短了一点:

import pandas as pd
from datetime import date
url = 'https://raw.githubusercontent.com/esheehan1/projects/master/BB_FUT_DATA.csv'
df = pd.read_csv(url, usecols=['Dates','TYAFWD Comdty'], index_col=['Dates'], skiprows=None, parse_dates=True, dayfirst=True, nrows=1500)
print(df.iloc[15:20,:])

            TYAFWD Comdty
Dates                    
2020-05-05          0.541
2020-05-04          0.527
2020-05-01          0.512
2020-04-30          0.528
2020-04-29          0.521
添加所需的列:

df['year']  = pd.to_datetime(df.index).year
df['month'] = pd.to_datetime(df.index).month
df['week']  = pd.to_datetime(df.index).weekofyear
print(df.iloc[15:20,:])

            TYAFWD Comdty  year  month  week
Dates                                       
2020-05-05          0.541  2020      5    19
2020-05-04          0.527  2020      5    19
2020-05-01          0.512  2020      5    18
2020-04-30          0.528  2020      4    18
2020-04-29          0.521  2020      4    18

查看pd.read\u csv,您可能会发现许多有用的参数

pd.read\u csv中的默认日期时间格式会导致您的问题,因为它假定分隔格式为%m/%d/%Y。我还建议您稍微简化代码,因为目前有许多不必要的强制转换操作:

作为pd进口熊猫 结果已经是一个数据帧 df=pd.read_csv'BB_FUT_DATA.csv',usecols=['Dates','TYAFWD Comdty',skiprows=None,nrows=1500 df.dropnainplace=True df.Dates=pd.to_datetimedf.Dates,格式='%d/%m/%Y' df.set_索引'Dates',in place=True 由于df.index已经是datetime类型,您可以直接访问年、月、周和年属性 df['year']=df.index.year df['month']=df.index.month df['week']=df.index.weekofyear printdf.head30 或者,您可以在pd.read\u csv中完全执行此操作,使用@eNc指出的dayfirst=True或date\u parser=lambda x:pd.to\u datetimex,format='%d/%m/%Y'和na\u过滤器删除具有NaN和na值的行:

作为pd进口熊猫 df=pd.read\u csv “BB_FUT_DATA.csv”, usecols=['Dates','TYAFWD Comdty',], parse_dates=True, dayfirst=正确, skiprows=无, nrows=1500, 索引col='Dates', na_过滤器=真 df['year']=df.index.year df['month']=df.index.month df['week']=df.index.weekofyear printdf.head30 输出:

            TYAFWD Comdty  year  month  week
Dates                                       
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-05-12          0.510  2020      5    20
2020-05-11          0.548  2020      5    20
2020-05-08          0.527  2020      5    19
2020-05-07          0.494  2020      5    19
2020-05-06          0.568  2020      5    19
2020-05-05          0.541  2020      5    19
2020-05-04          0.527  2020      5    19
2020-05-01          0.512  2020      5    18
2020-04-30          0.528  2020      4    18
2020-04-29          0.521  2020      4    18
2020-04-28          0.519  2020      4    18
2020-04-27          0.559  2020      4    18
2020-04-24          0.518  2020      4    17
2020-04-23          0.512  2020      4    17
2020-04-22          0.514  2020      4    17
2020-04-21          0.474  2020      4    17
2020-04-20          0.490  2020      4    17
2020-04-17          0.521  2020      4    16
2020-04-16          0.510  2020      4    16
2020-04-15          0.498  2020      4    16

怎么了?你希望得到什么结果?对不起,我不太清楚。我希望输出中的所有条目都在5月5日,但是一旦你发布了数据,就不可能在不查看数据的情况下回答。下面是Github上原始数据的路径。这回答了你的问题吗?
df['year']  = pd.to_datetime(df.index).year
df['month'] = pd.to_datetime(df.index).month
df['week']  = pd.to_datetime(df.index).weekofyear
print(df.iloc[15:20,:])

            TYAFWD Comdty  year  month  week
Dates                                       
2020-05-05          0.541  2020      5    19
2020-05-04          0.527  2020      5    19
2020-05-01          0.512  2020      5    18
2020-04-30          0.528  2020      4    18
2020-04-29          0.521  2020      4    18
            TYAFWD Comdty  year  month  week
Dates                                       
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-05-12          0.510  2020      5    20
2020-05-11          0.548  2020      5    20
2020-05-08          0.527  2020      5    19
2020-05-07          0.494  2020      5    19
2020-05-06          0.568  2020      5    19
2020-05-05          0.541  2020      5    19
2020-05-04          0.527  2020      5    19
2020-05-01          0.512  2020      5    18
2020-04-30          0.528  2020      4    18
2020-04-29          0.521  2020      4    18
2020-04-28          0.519  2020      4    18
2020-04-27          0.559  2020      4    18
2020-04-24          0.518  2020      4    17
2020-04-23          0.512  2020      4    17
2020-04-22          0.514  2020      4    17
2020-04-21          0.474  2020      4    17
2020-04-20          0.490  2020      4    17
2020-04-17          0.521  2020      4    16
2020-04-16          0.510  2020      4    16
2020-04-15          0.498  2020      4    16