我正在嘗試將日期轉換為Year-Quarter格式。下面是我的代碼
import pandas as pd
import datetime as datetime
pd.PeriodIndex(datetime.date(2020, 6, 15), freq='Q-MAR').strftime('Q%q')
我得到以下錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/pandas/core/indexes/period.py", line 241, in __new__
data = period_array(data=data, freq=freq)
File "/usr/local/lib/python3.9/site-packages/pandas/core/arrays/period.py", line 892, in period_array
data = list(data)
TypeError: 'datetime.date' object is not iterable
你能幫助如何糾正這個錯誤嗎?我希望得到的結果為2020-Q2.
uj5u.com熱心網友回復:
如果使用標量需要Period:
print (pd.Period(datetime.date(2020, 6, 15), freq='Q-MAR').strftime('Q%q'))
Q1
print (pd.Period(datetime.date(2020, 6, 15), freq='Q-MAR').strftime('%Y-Q%q'))
2020-Q1
print (pd.Period(datetime.date(2020, 6, 15), freq='Q').strftime('%Y-Q%q'))
2020-Q2
或[]為一個元素串列添加:
print (pd.PeriodIndex([datetime.date(2020, 6, 15)], freq='Q-MAR').strftime('Q%q'))
Index(['Q1'], dtype='object')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409566.html
標籤:
上一篇:創建一系列季度
