Python Pandas入門:DateTimeIndexに対して分単位の頻度でフロア操作を実行する方法
DateTimeIndexに対して分単位(minute)の頻度でフロア操作を実行するには、DateTimeIndex.floor()メソッドを使用します。分単位の頻度を指定する場合は、freqパラメータに'T'を設定します。
必要なライブラリのインポート
まず、必要なライブラリをインポートします。
import pandas as pd
DatetimeIndexの作成
次に、5つの要素を持ち、頻度が秒単位('40S'=40秒間隔)のDatetimeIndexを作成します。タイムゾーンは「Australia/Adelaide」に設定しています。
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')DateTimeIndexの表示
作成したDateTimeIndexを表示します。
print("DateTimeIndex...\n", datetimeindex)分単位のフロア操作の実行
DateTimeIndexの日時に対して、分単位の頻度でフロア操作を実行します。分単位の頻度には'T'を使用します。
print("\nPerforming floor operation with minute frequency...\n",
datetimeindex.floor(freq='T'))サンプルコード全体
以下が完全なコード例です。
import pandas as pd
# 5つの要素を持ち、頻度が秒単位(40秒間隔)のDatetimeIndexを作成
# タイムゾーンは Australia/Adelaide
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
# DateTimeIndexを表示
print("DateTimeIndex...\n", datetimeindex)
# DateTimeIndexの頻度を表示
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# 「分」の値を取得
res = datetimeindex.minute
# 分のみを表示
print("\nThe minute from DateTimeIndex...\n", res)
# DateTimeIndexに対して分単位の頻度でフロア操作を実行
# 分単位の頻度には 'T' を使用
print("\nPerforming floor operation with minute frequency...\n",
datetimeindex.floor(freq='T'))実行結果
上記のコードを実行すると、以下の出力が得られます。
DateTimeIndex... DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30', '2021-10-18 07:21:12.261811624+10:30', '2021-10-18 07:21:52.261811624+10:30', '2021-10-18 07:22:32.261811624+10:30', '2021-10-18 07:23:12.261811624+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='40S') DateTimeIndex frequency... <40 * Seconds> The minute from DateTimeIndex... Int64Index([20, 21, 21, 22, 23], dtype='int64') Performing floor operation with minute frequency... DatetimeIndex(['2021-10-18 07:20:00+10:30', '2021-10-18 07:21:00+10:30', '2021-10-18 07:21:00+10:30', '2021-10-18 07:22:00+10:30', '2021-10-18 07:23:00+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)
解説
実行結果からわかるように、floor(freq='T')を適用することで、各タイムスタンプの秒以下の値が切り捨てられ、直近の分単位の時刻に丸められています。たとえば「07:20:32.261811624」は「07:20:00」に、「07:21:12.261811624」は「07:21:00」になります。
なお、floor()は指定した頻度より細かい単位を切り捨てる処理であるため、元のデータの頻度情報(freq)は失われ、結果のDatetimeIndexではfreq=Noneになっている点にも注意してください。同様の操作として、切り上げを行うceil()や、四捨五入を行うround()も用意されています。
-
Python Pandas入門:DateTimeIndexを分単位の頻度で丸める方法
pandasでDateTimeIndex(日時インデックス)を分単位の頻度で丸めるには、DateTimeIndex.round()メソッドを使用します。分単位の頻度を指定したい場合は、freq引数にTを設定します。なお、Tは「min(minutes)」のエイリアスであり、どちらを指定しても同じ結果が得られます。手順1:ライブラリのインポートまず、必要なライブラリをインポートします。import pandas as pd手順2:DateTimeIndexの作成次に、期間5・頻度45秒(45s)のDatetimeIndexを作成します。タイムゾーンにはオーストラリアのアデレード(Australia
-
PandasでDateTimeIndexを1時間ごとの頻度で丸める方法|round()の使い方を解説
PandasでDateTimeIndexを1時間ごとの頻度に丸めるには、DateTimeIndex.round()メソッドを使用します。1時間ごとの頻度を指定する場合は、freqパラメータに「H」を設定します。 実装の手順 まず、必要なライブラリをインポートします。 import pandas as pd 続いて、期間5つ・頻度「T」(分単位)でDatetimeIndexを作成します。タイムゾーンはオーストラリア/アデレード(Australia/Adelaide)を指定しています。 datetimeindex = pd.date_range(2021-09-29 07:00, per