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

PythonPandas-1時間ごとの頻度でDateTimeIndexに対してceil操作を実行する方法


1時間ごとの頻度でDateTimeIndexに対してceil操作を実行するには、 DateTimeIndex.ceil()を使用します。 方法。 1時間ごとの頻度については、値「H」のfreqパラメーターを使用します。

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

import pandas as pd

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

datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='20min')

1時間ごとの頻度でDateTimeIndex日付にCeil操作を実行します。時間ごとの頻度には、「H」-

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

以下はコードです-

import pandas as pd

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

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

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

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

出力

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

DateTimeIndex...
DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30',
'2021-09-29 07:40:32.261811624+09:30',
'2021-09-29 08:00:32.261811624+09:30',
'2021-09-29 08:20:32.261811624+09:30',
'2021-09-29 08:40:32.261811624+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='20T')
DateTimeIndex frequency...
<20 * Minutes>

Performing ceil operation with hourly frequency...
DatetimeIndex(['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',
'2021-09-29 09:00:00+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)

  1. Pythonパンダ-分頻度でDateTimeIndexを丸める方法

    DateTimeIndexを分頻度で丸めるには、 DateTimeIndex.round()を使用します 方法。分頻度の場合は、頻度を使用します 値が‘T’のパラメータ 。 まず、必要なライブラリをインポートします- import pandas as pd 期間5、頻度をs、つまり秒とするDatetimeIndex。タイムゾーンはオーストラリア/アデレード- datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='45s&#

  2. 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'