Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

python - Pandas datetime column to ordinal

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use apply:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...