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

PythonPandas-指定されたDateOffsetオブジェクトに適用された頻度を返します


指定されたDateOffsetオブジェクトに適用された頻度を返すには、Pandasでoffset.freqstrを使用します。まず、必要なライブラリをインポートします-

from pandas.tseries.frequencies import to_offset
import pandas as pd

パンダでタイムスタンプオブジェクトを設定します-

timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

DateOffsetを作成します。ここでは、「D」頻度を使用して日数を増やしています-

offset = to_offset("5D")

更新されたタイムスタンプを表示する-

print("\nUpdated Timestamp...\n",timestamp + offset)

指定されたDateOffsetオブジェクトに適用された頻度を返します-

print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

以下はコードです-

from pandas.tseries.frequencies import to_offset
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the DateOffset
# We are incrementing the days here using the "D" frequency
offset = to_offset("5D")

# Display the DateOffset
print("\nDateOffset...\n",offset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)

# return the frequency applied on the given DateOffset object
print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

出力

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

Timestamp...
 2021-09-26 03:25:02.000045

DateOffset...
 <5 * Days>

Updated Timestamp...
 2021-10-01 03:25:02.000045

The frequency on the DateOffset object..
 5D

  1. PythonPandas-Periodオブジェクトを月次頻度のタイムスタンプとして返​​します

    Periodオブジェクトを月次頻度のタイムスタンプとして返​​すには、 period.to_timestamp()を使用します メソッドを実行し、freqパラメータを「 M」として設定します ’。 まず、必要なライブラリをインポートします- import pandas as pd pandas.Periodは期間を表します。期間オブジェクトの作成 period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45) 期間オブジェクトを表示

  2. PythonPandas-指定されたPeriodオブジェクトに適用された時系列頻度の文字列エイリアスを返します

    指定されたPeriodオブジェクトに適用された時系列頻度の文字列エイリアスを返すには、 period.freqstrを使用します プロパティ。 まず、必要なライブラリをインポートします- import pandas as pd pandas.Periodは期間を表します。 2つのPeriodオブジェクトの作成- period1 = pd.Period("2020-09-23 03:55:20") period2 = pd.Period(freq="Y", year = 2021, month = 2, day = 14, hour = 2, minu