4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pandasのdatetimeの丸めの基準はどこか

Posted at

忙しい人のための回答

1970年1月1日0時0分0秒 っぽい

はじめに

pandas では roundやfloorを用いで日付を丸めることが出来ます.
それは1分や1時間のような単位だけでなく,5分毎に丸める,といった操作も可能です.
5分ごとにまとめる場合は,例えば次のようにします.

sample1.py

import pandas as pd

dates = pd.date_range('1/1/2021 00:00:00', periods=10, freq='min')
print("original date:")
print(dates)
print("floor date:")
print(dates.floor('5T'))
実行結果

original date:
DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 00:01:00',
               '2021-01-01 00:02:00', '2021-01-01 00:03:00',
               '2021-01-01 00:04:00', '2021-01-01 00:05:00',
               '2021-01-01 00:06:00', '2021-01-01 00:07:00',
               '2021-01-01 00:08:00', '2021-01-01 00:09:00'],
              dtype='datetime64[ns]', freq='T')

floor date:
DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 00:00:00',
               '2021-01-01 00:00:00', '2021-01-01 00:00:00',
               '2021-01-01 00:00:00', '2021-01-01 00:05:00',
               '2021-01-01 00:05:00', '2021-01-01 00:05:00',
               '2021-01-01 00:05:00', '2021-01-01 00:05:00'],
              dtype='datetime64[ns]', freq=None)

ここで気になるのが,5分のようにキリの良い数字ではなく,7分のようなキリの悪い数字を使った際の挙動です.

検証

例えばpandas.Series.dt.floorのreferenceを読んでも

Perform floor operation on the data to the specified freq.

としか書いてなかったので,実際にやってみます.
2021年年始と1970年年始で挙動を確認してみます.

sample2.py

import pandas as pd

dates = pd.date_range('1/1/2021 00:00:00', periods=14, freq='min')
print("original date:")
print(dates)
print("floor date:")
print(dates.floor('7T'))

dates = pd.date_range('1/1/1970 00:00:00', periods=14, freq='min')
print("original date:")
print(dates)
print("floor date:")
print(dates.floor('7T'))
実行結果
original date:
DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 00:01:00',
               '2021-01-01 00:02:00', '2021-01-01 00:03:00',
               '2021-01-01 00:04:00', '2021-01-01 00:05:00',
               '2021-01-01 00:06:00', '2021-01-01 00:07:00',
               '2021-01-01 00:08:00', '2021-01-01 00:09:00',
               '2021-01-01 00:10:00', '2021-01-01 00:11:00',
               '2021-01-01 00:12:00', '2021-01-01 00:13:00'],
              dtype='datetime64[ns]', freq='T')

floor date:
DatetimeIndex(['2020-12-31 23:55:00', '2020-12-31 23:55:00',
               '2021-01-01 00:02:00', '2021-01-01 00:02:00',
               '2021-01-01 00:02:00', '2021-01-01 00:02:00',
               '2021-01-01 00:02:00', '2021-01-01 00:02:00',
               '2021-01-01 00:02:00', '2021-01-01 00:09:00',
               '2021-01-01 00:09:00', '2021-01-01 00:09:00',
               '2021-01-01 00:09:00', '2021-01-01 00:09:00'],
              dtype='datetime64[ns]', freq=None)

original date:
DatetimeIndex(['1970-01-01 00:00:00', '1970-01-01 00:01:00',
               '1970-01-01 00:02:00', '1970-01-01 00:03:00',
               '1970-01-01 00:04:00', '1970-01-01 00:05:00',
               '1970-01-01 00:06:00', '1970-01-01 00:07:00',
               '1970-01-01 00:08:00', '1970-01-01 00:09:00',
               '1970-01-01 00:10:00', '1970-01-01 00:11:00',
               '1970-01-01 00:12:00', '1970-01-01 00:13:00'],
              dtype='datetime64[ns]', freq='T')

floor date:
DatetimeIndex(['1970-01-01 00:00:00', '1970-01-01 00:00:00',
               '1970-01-01 00:00:00', '1970-01-01 00:00:00',
               '1970-01-01 00:00:00', '1970-01-01 00:00:00',
               '1970-01-01 00:00:00', '1970-01-01 00:07:00',
               '1970-01-01 00:07:00', '1970-01-01 00:07:00',
               '1970-01-01 00:07:00', '1970-01-01 00:07:00',
               '1970-01-01 00:07:00', '1970-01-01 00:07:00'],
              dtype='datetime64[ns]', freq=None)

やはりというべきか,(毎年の年始の0時0分のような基準ではなく)1970年1月1日0時0分基準のようですね.

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?