Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonパンダ-1時間ごとの頻度でDateTimeIndexを丸める方法


DateTimeIndexを1時間ごとの頻度で丸めるには、 DateTimeIndex.round()を使用します 方法。時間ごとの頻度については、頻度を使用してください 値が「H」のパラメータ。

まず、必要なライブラリをインポートします-

import pandas as pd

期間5、頻度をT、つまり分としてDatetimeIndexを作成します-

datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T')

DateTimeIndexを表示-

print("DateTimeIndex...\n", datetimeindex)

1時間ごとの頻度でDateTimeIndex日付のラウンド操作。時間ごとの頻度には、「H」-

を使用しました。
print("\nPerforming round operation with hourly frequency...\n",
datetimeindex.round(freq='H'))

以下はコードです-

import pandas as pd

# DatetimeIndex with period 5 and frequency as T i.e. minutes
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T')

# display
print("DateTimeIndex...\n", datetimeindex)

# getting the hour
res = datetimeindex.hour
# display only the hour
print("\nThe hour from DateTimeIndex...\n", res)

# Round operation on DateTimeIndex date with hourly frequency
# For hourly frequency, we have used 'H'
print("\nPerforming round operation with hourly frequency...\n",
datetimeindex.round(freq='H'))

出力

これにより、次のコードが生成されます-

DateTimeIndex...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:35:00+09:30',
'2021-09-29 08:10:00+09:30', '2021-09-29 08:45:00+09:30',
'2021-09-29 09:20:00+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='35T')

The hour from DateTimeIndex...
Int64Index([7, 7, 8, 8, 9], dtype='int64')

Performing round operation with hourly frequency...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 08:00:00+09:30',
'2021-09-29 08:00:00+09:30', '2021-09-29 09:00:00+09:30',
'2021-09-29 09:00:00+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)

  1. PythonPandas-タイムデルタを秒の頻度で丸めます

    指定された解像度でTimedeltaを丸めるには、 timestamp.round()を使用します 方法。 freqを使用して秒の周波数分解能を設定します 値が‘s’のパラメータ 。 まず、必要なライブラリをインポートします- import pandas as pd Timedeltaオブジェクトを作成する- timedelta = pd.Timedelta('1 days 11 hours 22 min 25 s 50 ms 45 ns') タイムデルタを表示- print("Timedelta...\n", timedelta) 秒の頻度で丸

  2. Pythonパンダ-1時間ごとの頻度でTimedeltaを丸める

    指定された解像度でTimedeltaを丸めるには、 timestamp.round()を使用します 方法。値Hのfreqパラメータを使用して、1時間ごとの周波数分解能を設定します。 まず、必要なライブラリをインポートします- import pandas as pd TimeDeltasは、Pythonの標準の日時ライブラリであり、異なる表現のtimedeltaを使用します。 Timedeltaオブジェクトを作成する timedelta = pd.Timedelta('2 days 10 hours 45 min 20 s 35 ms 55 ns') タイムデルタを表示する