pandas 熊猫日期时间列到序数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20576618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:26:51  来源:igfitidea点击:

Pandas datetime column to ordinal

pythondatetimepandas

提问by Rich Signell

I'm trying to create a new Pandas dataframe column with ordinal day from a datetime column:

我正在尝试从日期时间列创建一个新的 Pandas 数据框列,其中包含序号:

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'

I guess this is a basic question, but I've struggled reading docs for last 30 minutes.

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

How can I create an ordinal time column for my dataframe?

如何为我的数据框创建顺序时间列?

回答by lowtech

Use apply:

使用申请:

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

回答by behzad.nouri

you can also use map:

您还可以使用map

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