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

PythonPandas-指定されたDateOffsetオブジェクトに適用されたルールコードを返します


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

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

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

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

DateOffsetを作成します。ここでは、「M」頻度を使用して月をインクリメントしています-

offset = to_offset("3M")

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

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

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

print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

以下はコードです-

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 months here using the "M" frequency
offset = to_offset("3M")

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

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

# return the rule code of the frequency applied on the given DateOffset object
print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

出力

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

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

DateOffset...
 <3 * MonthEnds>

Updated Timestamp...
 2021-11-30 03:25:02.000045

The rule code of the DateOffset object..
 M

  1. PythonPandas-指定されたDateOffsetオブジェクトのナノ秒数を返します

    指定されたDateOffsetオブジェクトのナノ秒数を返すには、Pandasのoffset.nanosプロパティを使用します。 まず、必要なライブラリをインポートします- from pandas.tseries.frequencies import to_offset import pandas as pd パンダでタイムスタンプオブジェクトを設定します- timestamp = pd.Timestamp('2021-08-30 03:08:02.000045') DateOffsetを作成します。ここでは、「D」頻度を使用して日数を増やしています- offset =

  2. PythonPandas-指定されたDateOffsetオブジェクトに適用される頻度を文字列として返します

    指定されたDateOffsetオブジェクトに適用された頻度を文字列として返すには、 offset.freqstrを使用します パンダのプロパティ。 まず、必要なライブラリをインポートします- from pandas.tseries.offsets import DateOffset import pandas as pd パンダでタイムスタンプオブジェクトを設定します- timestamp = pd.Timestamp('2021-08-30 02:30:55') DateOffsetを作成します。ここでは、「months」パラメータを使用して月をインクリメントしていま