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

PythonPandas-指定されたDateOffsetオブジェクトに適用された増分の数を返します


指定されたDateOffsetオブジェクトに適用された増分のカウントを返すには、Pandasのoffset.nプロパティを使用します。まず、必要なライブラリをインポートします-

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("5M")

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

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

指定されたDateOffsetオブジェクトの増分数を返します-

print("\nThe count of increments on the DateOffset object..\n", offset.n)

以下はコードです-

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("5M")

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

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

# return the count of increments on the given DateOffset object
print("\nThe count of increments on the DateOffset object..\n", offset.n)

出力

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

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

DateOffset...
 <5 * MonthEnds>

Updated Timestamp...
 2022-01-31 03:25:02.000045

The count of increments on the DateOffset object..
 5

  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」パラメータを使用して月をインクリメントしていま