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

PythonPandas-指定されたタイムスタンプを期間に変換する


指定されたタイムスタンプを期間に変換するには、タイムスタンプ.to_period()を使用します 方法。その中で、 freqを使用して頻度を設定します パラメータ。

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

import pandas as pd

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

timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

次に、タイムスタンプを期間に変換します。値が「M」の「freq」パラメータを使用して、頻度を月に設定しました

timestamp.to_period(freq='M')

以下はコードです

import pandas as pd

# set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

# display the Timestamp
print("Timestamp...\n", timestamp)

# convert timestamp to Period
# we have set the frequency as Month using the "freq" parameter with value 'M'
print("\nTimestamp to Period...\n", timestamp.to_period(freq='M'))
の"freq"パラメーターを使用して頻度をMonthに設定しました。

出力

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

Timestamp...
 2021-09-14 15:12:34.261811624

Timestamp to Period...
2021-09

  1. Pythonでのタイムスタンプの比較–パンダ

    タイムスタンプを比較するために、インデックス演算子、つまり角かっこを使用できます。まず、必要なライブラリをインポートします- import pandas as pd 3列のデータフレームを作成する- dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"],       &

  2. 文字列辞書をPythonの辞書に変換する

    この記事では、文字列を含む特定の辞書を、キーと値のペアの通常の辞書に変換する方法を説明します。 json.loadsを使用 json.loadsは指定された文字列を渡すことができ、データの構造を保持する通常の文字列として結果を提供します。したがって、指定された文字列ディクショナリをパラメータとしてこの関数に渡し、結果を取得します。 例 import json stringA = '{"Mon" : 3, "Wed" : 5, "Fri" : 7}' # Given string dictionary print(&q