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

PythonPandas-DateTimeIndexの日付が月の最後の日であるかどうかを示します


DateTimeIndexの日付が月の最後の日であるかどうかを確認するには、 DateTimeIndex.is_month_endを使用します。 プロパティ。

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

import pandas as pd

期間6、頻度をD、つまり日数としてDatetimeIndexを作成します-

datetimeindex = pd.date_range('2021-9-15 06:40:35', periods=6, tz='Australia/Adelaide', freq='15D')

DateTimeIndexを表示-

print("DateTimeIndex...\n", datetimeindex)

DateTimeIndexの日付が月の最後の日であるかどうかを確認します-

print("\nCheck whether the date in DateTimeIndex is the last day of the month...\n",
datetimeindex.is_month_end)

以下はコードです-

import pandas as pd

# DatetimeIndex with period 6 and frequency as D i.e. days
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-9-15 06:40:35', periods=6, tz='Australia/Adelaide', freq='15D')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# Check whether the date in DateTimeIndex is the last day of the month
print("\nCheck whether the date in DateTimeIndex is the last day of the month...\n",
datetimeindex.is_month_end)

出力

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

DateTimeIndex...
DatetimeIndex(['2021-09-15 06:40:35+09:30', '2021-09-30 06:40:35+09:30',
'2021-10-15 06:40:35+10:30', '2021-10-30 06:40:35+10:30',
'2021-11-14 06:40:35+10:30', '2021-11-29 06:40:35+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='15D')
DateTimeIndex frequency...
<15 * Days>

Check whether the date in DateTimeIndex is the last day of the month...
[False True False False False False]

  1. Python-Pandasシリーズの最後の要素にアクセスするにはどうすればよいですか?

    iatを使用します 整数位置で行/列ペアの単一の値にアクセスするために使用されるため、最後の要素にアクセスするための属性。 まず、必要なパンダライブラリをインポートしましょう- import pandas as pd 数字でパンダシリーズを作成する- data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100]) ここで、iat()-を使用して最後の要素を取得します data.iat[-1] 例 以下はコードです- import pandas as pd # pandas series data = pd.Series([10, 20, 5, 6

  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) 出力 これにより出力が得られます