python - Convert Pandas DatetimeIndex to fractional day of year -


is there easy way convert datetimeindex array of day of years, including fraction hour, minute, etc. components?

for example, converting pd.date_range("2014-01-01 00:00", periods=4, freq="12h") should give me [1.0, 1.5, 2.0, 2.5].

this requires 0.15.0 timedelta functionaility. have full precision of dates.

in [19]: s out[19]:  <class 'pandas.tseries.index.datetimeindex'> [2014-01-01 00:00:00, ..., 2014-01-02 12:00:00] length: 4, freq: 12h, timezone: none  in [21]: s-s[0] out[21]:  <class 'pandas.tseries.tdi.timedeltaindex'> ['0 days 00:00:00', ..., '1 days 12:00:00'] length: 4, freq: none  in [22]: ((s-s[0]) / pd.timedelta(1.0,unit='d')) + 1 out[22]: float64index([1.0, 1.5, 2.0, 2.5], dtype='float64') 

dividing timedeltaindex timedelta gives (float) fraction. dates 0 based add 1.

the above 'assumes' of dates in same year. here more robust way (datetimeindex subtraction not directly supported)

in [53]: pd.timedeltaindex(s.asi8-s.to_period('a').to_timestamp().asi8) out[53]:  <class 'pandas.tseries.tdi.timedeltaindex'> ['0 days 00:00:00', ..., '1 days 12:00:00'] length: 4, freq: none 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -