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

PythonPandas-DateTimeIndexの日付が四半期の最終日であるかどうかを示します


DateTimeIndexの日付が四半期の最終日であるかどうかを確認するには、 DateTimeIndex.is_quarter_endを使用します。 プロパティ。

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

import pandas as pd

期間6、頻度をD、つまり日数としてDatetimeIndexを作成します。タイムゾーンはオーストラリア/アデレード-

datetimeindex = pd.date_range('2021-6-15 02:30:50', 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 quarter...\n",
datetimeindex.is_quarter_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-6-15 02:30:50', 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 quarter
# Result is based on the following quarters of an year:
# Quarter 1 = 1st January to 31st March
# Quarter 2 = 1st April to 30th June
# Quarter 3 = 1st July to 30th September
# Quarter 4 = 1st October to 31st December
print("\nCheck whether the date in DateTimeIndex is the last day of the quarter...\n",
datetimeindex.is_quarter_end)

出力

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

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

Check whether the date in DateTimeIndex is the last day of the quarter...
[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の年間最優秀日

    「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