Python Pandas入門:TimedeltaIndex.componentsでタイムデルタの構成要素をDataFrameとして取得する方法
TimedeltaIndexの各要素を「日・時・分・秒・ミリ秒・マイクロ秒・ナノ秒」といった構成要素(コンポーネント)に分解し、その結果をDataFrameとして取得したい場合は、TimedeltaIndex.componentsプロパティを使用します。
この記事では、実際のコード例とともに、componentsプロパティの使い方と出力結果をわかりやすく解説します。
必要なライブラリのインポート
まず、pandasをインポートします。
import pandas as pd
TimedeltaIndexオブジェクトの作成
次に、TimedeltaIndexオブジェクトを作成します。dataパラメータに、timedelta形式の文字列のリストを渡すことで、複数の時間差データを一度に扱えます。
tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 35s 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])
TimedeltaIndexの表示
作成したTimedeltaIndexの中身を確認してみましょう。
print("TimedeltaIndex...\n", tdIndex)
コンポーネントのDataFrameを取得する
tdIndex.componentsを実行すると、各タイムデルタを日数・時間・分・秒・ミリ秒・マイクロ秒・ナノ秒に分解した結果がDataFrameとして返されます。
print("\nThe Dataframe of the components of TimeDeltas...\n", tdIndex.components)
完全なコード例
以下は、days・seconds・microsecondsなどの個別プロパティと、componentsプロパティを組み合わせたサンプルコードです。
import pandas as pd
# TimedeltaIndexオブジェクトを作成
# 'data'パラメータにtimedelta形式のデータを指定
tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 35s 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+21:15:45.999999'])
# TimedeltaIndexを表示
print("TimedeltaIndex...\n", tdIndex)
# 各要素の日数を表示
print("\nThe number of days from the TimeDeltaIndex object...\n", tdIndex.days)
# 各要素の秒数を表示
print("\nThe number of seconds from the TimeDeltaIndex object...\n", tdIndex.seconds)
# 各要素のマイクロ秒数を表示
print("\nThe number of microseconds from the TimeDeltaIndex object...\n", tdIndex.microseconds)
# タイムデルタのコンポーネントからなるDataFrameを返す
# コンポーネントには 日・時・分・秒・ミリ秒・マイクロ秒・ナノ秒 が含まれる
print("\nThe Dataframe of the components of TimeDeltas...\n", tdIndex.components)
実行結果
上記のコードを実行すると、次のような出力が得られます。
TimedeltaIndex... TimedeltaIndex(['10 days 05:02:35.000003010', '0 days 22:39:19.999999', '2 days 07:08:02.000045', '0 days 21:15:45.999999'], dtype='timedelta64[ns]', freq=None) The number of days from the TimeDeltaIndex object... Int64Index([10, 0, 2, 0], dtype='int64') The number of seconds from the TimeDeltaIndex object... Int64Index([18155, 81559, 25682, 76545], dtype='int64') The number of microseconds from the TimeDeltaIndex object... Int64Index([3, 999999, 45, 999999], dtype='int64') The Dataframe of the components of TimeDeltas... days hours minutes seconds milliseconds microseconds nanoseconds 0 10 5 2 35 0 3 10 1 0 22 39 19 999 999 0 2 2 7 8 2 0 45 0 3 0 21 15 45 999 999 0
まとめ
TimedeltaIndex.componentsを使うことで、複雑な時間差データを日・時・分・秒以下の単位ごとに整理されたDataFrameとして簡単に取得できます。また、.daysや.secondsなど個別のプロパティを使えば、特定の単位だけを抽出することも可能です。時系列データの分析や前処理において非常に便利な機能なので、ぜひ活用してみてください。
-
Python – Pandas DataFrameのデータを再形成する方法
Pandasでは、特定の列の値をカテゴリ化することで、DataFrameのデータを簡単に再形成(リシェイプ)できます。ここでは、「Result」列に含まれる「Pass(合格)」と「Fail(不合格)」という文字列の値を、数値形式に変換する方法を紹介します。 必要なライブラリのインポート まず、Pandasライブラリをインポートします。 import pandas as pd DataFrameの作成 次に、「Student」と「Result」の2つの列を持つDataFrameを作成します。 dataFrame = pd.DataFrame( { Student: [Ja
-
Python Pandas入門:query()メソッドでDataFrameの列をクエリ(条件抽出)する方法
PandasのDataFrameで列をクエリ(条件による絞り込み)を行うには、query()メソッドを使用します。query()は、文字列形式の条件式を渡すだけで直感的にレコードをフィルタリングできる便利な機能です。 サンプルDataFrameの作成 まず、商品名・期首在庫・期末在庫を持つDataFrameを作成してみましょう。 import pandas as pd dataFrame = pd.DataFrame({ Product: [SmartTV, PenDrive, Speaker, Earphone], Opening_Stock: [300, 700, 1