从CSV读取时,如何将数据框行索引更改为datetime.date?

问题描述

df.index[0]我想成为datetime.date(2006,8,27)

文件df = pd.read_csv(filePath,index_col="Date")df.index[0]读取时,显示为字符串'2006-08-27'

我尝试过:

dateparser = lambda s: datetime.datetime.strptime(s,"%Y-%m-%d").date()
df = pd.read_csv(filePath,parse_dates=["Date"],date_parser=dateparser,index_col="Date")

现在,df.index[0]显示Timestamp('2006-08-27 00:00:00')

如何将df.index[0]设置为datetime.date(2006,27)

二手csv示例:

Date,Symbol,Series,Prev Close,Open,High,Low,Last,Close,VWAP,Volume,Turnover,Trades,Deliverable Volume,%Deliverble
2006-08-27,2006-08-28,ATFC,EQ,365.0,521.0,569.0,502.0,553.0,554.25,552.0,15166163,837176013020000.0,3777529,0.24910000000000002
2006-08-29,555.0,563.9,535.55,536.1,539.3,547.59,3929113,215153038915000.0,727534,0.1852
2006-08-30,537.0,542.9,521.5,529.0,528.1,529.55,2034983,107762957620000.0,345064,0.1696
2006-08-31,525.0,544.0,515.0,539.35,538.45,532.89,1670990,89044643830000.0,286440,0.1714
2006-09-01,539.0,549.0,535.1,541.35,541.85,542.46,1176195,63803856150000.0,213842,0.1818

解决方法

代替使用lambda函数,已经有一个将数据更改为日期时间pd.to_datetime

的函数。

因此您可以执行以下操作:

df = pd.read_csv(filePath,index_col="Date")

df['Date'] = pd.to_datetime(df['Date'],format = '%Y-%m-%d')

df['Date'] = df['Date'].apply(lambda x : x.date())

print(type(df['Date'][0]))

输出

<class 'datetime.date'>

函数中还有一个format参数,以匹配您的数据 Format

我认为您的格式为format = '%Y-%m-%d'

,

根据pandas.read_csv,您还可以指定parse_dates = Trueinfer_datetime_format = True参数,以使大熊猫尝试从您设置为日期的索引中解析日期。 如:

df = pd.read_csv(filePath,index_col="Date",parse_dates=True,infer_datetime_format=True)
,

无法获得任何班轮。

df = pd.read_csv(filePath)   # load dataframe
df["Date"]=df["Date"].apply(lambda s: datetime.datetime.strptime(s,"%Y-%m-%d").date()) # convert Date column items to datetime.date
df.set_index('Date',inplace=True) # set Date as row index