Pandas datetime列到ordin

2024-09-30 06:30:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个新的Pandas dataframe列,其中包含datetime列中的ordinal day:

import pandas as pd
from datetime import datetime

print df.ix[0:5]
                              date
file                              
gom3_197801.nc 2011-02-16 00:00:00
gom3_197802.nc 2011-02-16 00:00:00
gom3_197803.nc 2011-02-15 00:00:00
gom3_197804.nc 2011-02-17 00:00:00
gom3_197805.nc 2011-11-14 00:00:00

df['date'][0].toordinal()

Out[6]:
734184

df['date'].toordinal()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-dbfd5e8b60f0> in <module>()
----> 1 df['date'].toordinal()

AttributeError: 'Series' object has no attribute 'toordinal'

我想这是一个基本的问题,但在过去的30分钟里,我一直在努力阅读文档。

如何为数据帧创建顺序时间列?


Tags: importdataframepandasdfdatetimedateaspd
2条回答

您还可以使用map

import datetime as dt
df['date'].map(dt.datetime.toordinal)

使用应用:

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

相关问题 更多 >

    热门问题