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

PythonPandas-生理が始まる月の日を取得します


期間が該当する月の日を取得するには、 period.dayを使用します プロパティ。

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

import pandas as pd

pandas.Periodは期間を表します。 2つのPeriodオブジェクトの作成-

period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)

2つのPeriodオブジェクトから月の日を取得します-

res1 = period1.day
res2 = period2.day

以下はコードです-

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)

# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

# get the day of the month from two Period objects
res1 = period1.day
res2 = period2.day

# Return the day of the month from the two Period objects
print("\nDay of the month from the 1st Period object ...\n", res1)
print("\nDay of the month from the 2nd Period object...\n", res2)

出力

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

Period1...
2021-09-18
Period2...
2021-09-22

Day of the month from the 1st Period object ...
18

Day of the month from the 2nd Period object...
22

  1. PythonPandas-期間が含まれる曜日を取得します

    期間が該当する曜日を取得するには、 period.dayofweekを使用します プロパティ まず、必要なライブラリをインポートします- import pandas as pd pandas.Periodは期間を表します。 2つのPeriodオブジェクトの作成- period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55) 期間オブジェクトを表示する- print(&quo

  2. Pythonで月の最後の日を取得するにはどうすればよいですか?

    カレンダーモジュールを使用して、月の最初の日の平日と月の日数を見つけることができます。この情報を使用すると、月の最終日を簡単に取得できます。 calenderモジュールには、指定された年と月の月の最初の日の平日と月の日数を返すメソッドmonthrange(year、month)があります。 例 import calendar day, num_days = calendar.monthrange(2017, 12) last_week = num_days % 7 last_day = (day + last_week) % 7 print(last_day) 出力 これにより出力が得られます