.dt访问器

Series has an accessor to succinctly return datetime like properties for the values of the Series, if it is a datetime/period like Series. This will return a Series, indexed like the existing Series.

  1. # datetime
  2. In [280]: s = pd.Series(pd.date_range('20130101 09:10:12', periods=4))
  3. In [281]: s
  4. Out[281]:
  5. 0 2013-01-01 09:10:12
  6. 1 2013-01-02 09:10:12
  7. 2 2013-01-03 09:10:12
  8. 3 2013-01-04 09:10:12
  9. dtype: datetime64[ns]
  10. In [282]: s.dt.hour
  11. Out[282]:
  12. 0 9
  13. 1 9
  14. 2 9
  15. 3 9
  16. dtype: int64
  17. In [283]: s.dt.second
  18. Out[283]:
  19. 0 12
  20. 1 12
  21. 2 12
  22. 3 12
  23. dtype: int64
  24. In [284]: s.dt.day
  25. Out[284]:
  26. 0 1
  27. 1 2
  28. 2 3
  29. 3 4
  30. dtype: int64

This enables nice expressions like this:

  1. In [285]: s[s.dt.day==2]
  2. Out[285]:
  3. 1 2013-01-02 09:10:12
  4. dtype: datetime64[ns]

You can easily produces tz aware transformations:

  1. In [286]: stz = s.dt.tz_localize('US/Eastern')
  2. In [287]: stz
  3. Out[287]:
  4. 0 2013-01-01 09:10:12-05:00
  5. 1 2013-01-02 09:10:12-05:00
  6. 2 2013-01-03 09:10:12-05:00
  7. 3 2013-01-04 09:10:12-05:00
  8. dtype: datetime64[ns, US/Eastern]
  9. In [288]: stz.dt.tz
  10. Out[288]: <DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>

You can also chain these types of operations:

  1. In [289]: s.dt.tz_localize('UTC').dt.tz_convert('US/Eastern')
  2. Out[289]:
  3. 0 2013-01-01 04:10:12-05:00
  4. 1 2013-01-02 04:10:12-05:00
  5. 2 2013-01-03 04:10:12-05:00
  6. 3 2013-01-04 04:10:12-05:00
  7. dtype: datetime64[ns, US/Eastern]

You can also format datetime values as strings with Series.dt.strftime() which supports the same format as the standard strftime().

  1. # DatetimeIndex
  2. In [290]: s = pd.Series(pd.date_range('20130101', periods=4))
  3. In [291]: s
  4. Out[291]:
  5. 0 2013-01-01
  6. 1 2013-01-02
  7. 2 2013-01-03
  8. 3 2013-01-04
  9. dtype: datetime64[ns]
  10. In [292]: s.dt.strftime('%Y/%m/%d')
  11. Out[292]:
  12. 0 2013/01/01
  13. 1 2013/01/02
  14. 2 2013/01/03
  15. 3 2013/01/04
  16. dtype: object
  1. # PeriodIndex
  2. In [293]: s = pd.Series(pd.period_range('20130101', periods=4))
  3. In [294]: s
  4. Out[294]:
  5. 0 2013-01-01
  6. 1 2013-01-02
  7. 2 2013-01-03
  8. 3 2013-01-04
  9. dtype: object
  10. In [295]: s.dt.strftime('%Y/%m/%d')
  11. Out[295]:
  12. 0 2013/01/01
  13. 1 2013/01/02
  14. 2 2013/01/03
  15. 3 2013/01/04
  16. dtype: object

The .dt accessor works for period and timedelta dtypes.

  1. # period
  2. In [296]: s = pd.Series(pd.period_range('20130101', periods=4, freq='D'))
  3. In [297]: s
  4. Out[297]:
  5. 0 2013-01-01
  6. 1 2013-01-02
  7. 2 2013-01-03
  8. 3 2013-01-04
  9. dtype: object
  10. In [298]: s.dt.year
  11. Out[298]:
  12. 0 2013
  13. 1 2013
  14. 2 2013
  15. 3 2013
  16. dtype: int64
  17. In [299]: s.dt.day
  18. Out[299]:
  19. 0 1
  20. 1 2
  21. 2 3
  22. 3 4
  23. dtype: int64
  1. # timedelta
  2. In [300]: s = pd.Series(pd.timedelta_range('1 day 00:00:05', periods=4, freq='s'))
  3. In [301]: s
  4. Out[301]:
  5. 0 1 days 00:00:05
  6. 1 1 days 00:00:06
  7. 2 1 days 00:00:07
  8. 3 1 days 00:00:08
  9. dtype: timedelta64[ns]
  10. In [302]: s.dt.days
  11. Out[302]:
  12. 0 1
  13. 1 1
  14. 2 1
  15. 3 1
  16. dtype: int64
  17. In [303]: s.dt.seconds
  18. Out[303]:
  19. 0 5
  20. 1 6
  21. 2 7
  22. 3 8
  23. dtype: int64
  24. In [304]: s.dt.components
  25. Out[304]:
  26. days hours minutes seconds milliseconds microseconds nanoseconds
  27. 0 1 0 0 5 0 0 0
  28. 1 1 0 0 6 0 0 0
  29. 2 1 0 0 7 0 0 0
  30. 3 1 0 0 8 0 0 0

Note: Series.dt will raise a TypeError if you access with a non-datetime-like values.