简体   繁体   中英

Pandas date_range and leap years

When running this code:

a = pd.date_range("1959-12-09 00:00:00", "2013-12-09 12:00:00", freq = "365D6H")
weekDays = [dt.datetime.weekday(d) for d in a]
df = pd.DataFrame({"Date": a, "Jour": weekDays})
df.head(6)

I'm getting:

0 1959-12-09 00:00:00     2
1 1960-12-08 06:00:00     3   * 
2 1961-12-08 12:00:00     4
3 1962-12-08 18:00:00     5
4 1963-12-09 00:00:00     0
5 1964-12-08 06:00:00     1   *
6 1965-12-08 12:00:00     2

and so problems with leap years. How could I do to have exactly one calendar year between dates in spite of leap years ?

Rather than use date_range , you could create this using a list comprehension:

In [11]: pd.to_datetime(["%s-12-09 %s:00:00" % (y, (6 * h) % 24)
                             for h, y in enumerate(xrange(1959, 2014))])
Out[11]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[1959-12-09 00:00:00, ..., 2013-12-09 12:00:00]
Length: 55, Freq: None, Timezone: None

The frequency is None, since this isn't a regular frequency... if you try and add a numpy year and a numpy hour you'll see:

In [21]: np.timedelta64(1, 'Y') + np.timedelta64(6, 'h')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-6a7f3e5b3315> in <module>()
----> 1 np.timedelta64(1, 'Y') + np.timedelta64(6, 'h')

TypeError: Cannot get a common metadata divisor for NumPy datetime metadata [Y] and [h] because they have incompatible nonlinear base time units

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM