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

Pythonパンダ-DateTimeIndexの日付が年の最後の日であるかどうかを示します


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

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

import pandas as pd

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

datetimeindex = pd.date_range('2021-12-25 02:30:50', periods=6, tz='Australia/Adelaide', freq='2D')

DateTimeIndexを表示-

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

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

print("\nCheck whether the date in DateTimeIndex is the last day of the year...\n", datetimeindex.is_year_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-12-25 02:30:50', periods=6, tz='Australia/Adelaide', freq='2D')

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

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

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

出力

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

DateTimeIndex...
DatetimeIndex(['2021-12-25 02:30:50+10:30', '2021-12-27 02:30:50+10:30',
'2021-12-29 02:30:50+10:30', '2021-12-31 02:30:50+10:30',
'2022-01-02 02:30:50+10:30', '2022-01-04 02:30:50+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='2D')
DateTimeIndex frequency...
<2 * Days>

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

  1. Pythonでプログラムを作成して、特定の日付シリーズの年の日を印刷します

    入力 − シリーズがあり、特定の日付範囲からその年の日を見つけたとします。 解決策 これを解決するために、以下のアプローチに従います。 シリーズを定義する date_rangeを「2020-01-10」として5つのピリオドで設定します。以下に定義されています pd.date_range('2020-01-10',periods=5) Series.dt.dayofyearを使用して日を検索します。 例 完全な実装を見て、理解を深めましょう- import pandas as pd date = pd.date_range('2020-0

  2. Pythonの年間最優秀日

    「YYYY-MM-DD」の形式の日付があるとします。その年の日数を返す必要があります。したがって、日付が「2019-02-10」の場合、これは1年の41日目です。 これを解決するには、次の手順に従います- Dが[0、31、28、31、30、31、30、31、31、30、31、30、31]のような日数の配列であるとします 日付を年、月、日のリストに変換します 年がうるう年の場合は、日付D [2]=29を設定します 月mm–1までの日数とそれ以降の日数を合計します。 例 理解を深めるために、次の実装を見てみましょう- class Solution(object):   &nbs