Pandas datetime with Julian Day

后端 未结 2 953
忘掉有多难
忘掉有多难 2020-12-21 22:06

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

相关标签:
2条回答
  • 2020-12-21 22:18

    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)

    0 讨论(0)
  • 2020-12-21 22:18

    Try dayofyear. Julian day is actually a completely different number FYI, see here

    In [1]: pd.date_range('20130201',periods=5).dayofyear
    Out[1]: array([32, 33, 34, 35, 36], dtype=int32)
    
    0 讨论(0)
提交回复
热议问题