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

PythonPandas-DateTimeIndexを使用して日時を作成します


日時を作成するには、date_range()を使用します。期間とタイムゾーンも頻度で設定されます。まず、必要なライブラリをインポートします-

import pandas as pd

期間8、頻度をM、つまり月とするDatetimeIndex。タイムゾーンはオーストラリア/シドニー-

datetime = pd.date_range('2021-09-24 02:35:55', periods=8, tz='Australia/Sydney', freq='M')

日時を表示-

print("DateTime...\n", datetime)

以下はコードです-

import pandas as pd

# DatetimeIndex with period 8 and frequency as M i.e. months
# timezone is Australia/Sydney
datetime = pd.date_range('2021-09-24 02:35:55', periods=8, tz='Australia/Sydney', freq='M')

# display
print("DateTime...\n", datetime)

# get the day name
print("\nGetting the day name..\n",datetime.day_name())

# get the month name
print("\nGetting the month name..\n",datetime.month_name())

# get the year
print("\nGetting the year name..\n",datetime.year)

# get the hour
print("\nGetting the hour..\n",datetime.hour)

# get the minutes
print("\nGetting the minutes..\n",datetime.minute)

# get the seconds
print("\nGetting the seconds..\n",datetime.second)

出力

これにより、次の出力が生成されます-

DateTime...
DatetimeIndex(['2021-09-30 02:35:55+10:00', '2021-10-31 02:35:55+11:00',
               '2021-11-30 02:35:55+11:00', '2021-12-31 02:35:55+11:00',
               '2022-01-31 02:35:55+11:00', '2022-02-28 02:35:55+11:00',
               '2022-03-31 02:35:55+11:00', '2022-04-30 02:35:55+10:00'],
               dtype='datetime64[ns, Australia/Sydney]', freq='M')

Getting the day name..
Index(['Thursday', 'Sunday', 'Tuesday', 'Friday', 'Monday', 'Monday','Thursday', 'Saturday'],
dtype='object')

Getting the month name..
Index(['September', 'October', 'November', 'December', 'January', 'February','March', 'April'], dtype='object')

Getting the year name..
   Int64Index([2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022], dtype='int64')

Getting the hour..
   Int64Index([2, 2, 2, 2, 2, 2, 2, 2], dtype='int64')

Getting the minutes..
   Int64Index([35, 35, 35, 35, 35, 35, 35, 35], dtype='int64')

Getting the seconds..
   Int64Index([55, 55, 55, 55, 55, 55, 55, 55], dtype='int64')

  1. SeaBornで箱ひげ図を作成する– Python Pandas

    Seabornの箱ひげ図は、カテゴリに関する分布を示す箱ひげ図を描画するために使用されます。これにはseaborn.boxplot()が使用されます。 以下がCSVファイル形式のデータセットであるとしましょう-Cricketers.csv まず、必要な3つのライブラリをインポートします- import seaborn as sb import pandas as pd import matplotlib.pyplot as plt CSVファイルからPandasDataFrameにデータをロードする- dataFrame = pd.read_csv("C:\\Users\\am

  2. SeaBornで散布図を作成する– Python Pandas

    SeabornのSactterPlotは、いくつかのセマンティックグループ化の可能性がある散布図を描画するために使用されます。これにはseaborn.scatterplot()が使用されます。 以下がCSVファイル形式のデータセットであるとしましょう-Cricketers.csv まず、必要な3つのライブラリをインポートします- import seaborn as sb import pandas as pd import matplotlib.pyplot as plt CSVファイルからPandasDataFrameにデータをロードする- dataFrame = pd.read_cs