Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Pandas datetime with Julian Day

I tried searching for this and was surprised I couldn't find anything. We use the term 'Julian Day' to refer to the day of the year irrespective of month (i.e. February 1st would be julian day 32). I don't know if this is a regional term and maybe why I can't find any answers.

Basically I have two files. One has a standard date format with year, month, day, hour. The other has year, julian day, hour. I am trying to align them using pandas DataFrame function and don't know what to do about the missing month data. Is Pandas able to convert this natively?

I am using python 3.3 and the newest version of Pandas.

Thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

When you read in the Julian date file, you simply need to provide a custom date parsing function. Here's an examples:

import datetime
from io import StringIO
import pandas

datafile = StringIO("""
jday,value
2013-01,1
2013-02,2
2013-100,8
2013-200,9
""")

dateparser = lambda x: datetime.datetime.strptime(x, '%Y-%j')
df = pandas.read_csv(datafile, parse_dates=True, date_parser=dateparser, index_col=[0])

Which gives a df of:

            value
jday             
2013-01-01      1
2013-01-02      2
2013-04-10      8
2013-07-19      9

I keep this page bookmarked and handy for "unconventional" date parsing needs such as these. (I don't actually think julian days are weird -- we use them all the time in hydraulic modeling)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...