尝试在 pandas 中获取 datetime64[ns] 的 'QS' 频率会给出属性错误
Trying to get 'QS' frequency in pandas for a datetime64[ns] gives attribute error
我正在使用外部数据源,我正在尝试获取特定数据字段的 Quaterstart(QS) 频率。我在下面提供了一个虚拟数据和代码。
import pandas as pd
df = pd.DataFrame(data=[['2022-01-01', '2021-01-03', 'a'], ['2020-05-01', '2021-03-03', 'b'],
['2023-06-02', '2019-04-03', 'c']], columns=['open_dt', 'd2', 'x'])
df['open_dt'] = df['open_dt'].astype('datetime64[ns]')
df['quater_open_dt'] = df['open_dt'].dt.to_period('QS')
我在 运行 这个
时遇到以下错误
---> 7 df['quater_open_dt'] = df['open_dt'].dt.to_period('QS')
8 df['establish_date'] = df['establish_date'].astype('datetime64[ns]')
9 df_tin = grp_on_tin(df)
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/accessor.py in f(self, *args, **kwargs)
90 def _create_delegator_method(name):
91 def f(self, *args, **kwargs):
---> 92 return self._delegate_method(name, *args, **kwargs)
93
94 f.__name__ = name
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/indexes/accessors.py in _delegate_method(self, name, *args, **kwargs)
107
108 method = getattr(values, name)
--> 109 result = method(*args, **kwargs)
110
111 if not is_list_like(result):
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/indexes/extension.py in method(self, *args, **kwargs)
81
82 def method(self, *args, **kwargs):
---> 83 result = attr(self._data, *args, **kwargs)
84 if wrap:
85 if isinstance(result, type(self._data)):
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/arrays/datetimes.py in to_period(self, freq)
1121 freq = res
1122
-> 1123 return PeriodArray._from_datetime64(self._data, freq, tz=self.tz)
1124
1125 def to_perioddelta(self, freq):
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/arrays/period.py in _from_datetime64(cls, data, freq, tz)
236 PeriodArray[freq]
237 """
--> 238 data, freq = dt64arr_to_periodarr(data, freq, tz)
239 return cls(data, freq=freq)
240
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/arrays/period.py in dt64arr_to_periodarr(data, freq, tz)
975 data = data._values
976
--> 977 base = freq._period_dtype_code
978 return c_dt64arr_to_periodarr(data.view("i8"), base, tz), freq
979
AttributeError: 'pandas._libs.tslibs.offsets.QuarterBegin' object has no attribute '_period_dtype_code'
有人可以帮助我了解这里发生的事情吗?
ps: 这里给出的数据是虚拟数据,不是原始数据
您的语法不正确,请改为尝试:
df["quater_open_dt"] = df["open_dt"].dt.to_period("Q").dt.start_time
print(df)
# Output
open_dt d2 x quater_open_dt
0 2022-01-01 2021-01-03 a 2022-01-01
1 2020-05-01 2021-03-03 b 2020-04-01
2 2023-06-02 2019-04-03 c 2023-04-01
我正在使用外部数据源,我正在尝试获取特定数据字段的 Quaterstart(QS) 频率。我在下面提供了一个虚拟数据和代码。
import pandas as pd
df = pd.DataFrame(data=[['2022-01-01', '2021-01-03', 'a'], ['2020-05-01', '2021-03-03', 'b'],
['2023-06-02', '2019-04-03', 'c']], columns=['open_dt', 'd2', 'x'])
df['open_dt'] = df['open_dt'].astype('datetime64[ns]')
df['quater_open_dt'] = df['open_dt'].dt.to_period('QS')
我在 运行 这个
时遇到以下错误---> 7 df['quater_open_dt'] = df['open_dt'].dt.to_period('QS')
8 df['establish_date'] = df['establish_date'].astype('datetime64[ns]')
9 df_tin = grp_on_tin(df)
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/accessor.py in f(self, *args, **kwargs)
90 def _create_delegator_method(name):
91 def f(self, *args, **kwargs):
---> 92 return self._delegate_method(name, *args, **kwargs)
93
94 f.__name__ = name
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/indexes/accessors.py in _delegate_method(self, name, *args, **kwargs)
107
108 method = getattr(values, name)
--> 109 result = method(*args, **kwargs)
110
111 if not is_list_like(result):
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/indexes/extension.py in method(self, *args, **kwargs)
81
82 def method(self, *args, **kwargs):
---> 83 result = attr(self._data, *args, **kwargs)
84 if wrap:
85 if isinstance(result, type(self._data)):
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/arrays/datetimes.py in to_period(self, freq)
1121 freq = res
1122
-> 1123 return PeriodArray._from_datetime64(self._data, freq, tz=self.tz)
1124
1125 def to_perioddelta(self, freq):
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/arrays/period.py in _from_datetime64(cls, data, freq, tz)
236 PeriodArray[freq]
237 """
--> 238 data, freq = dt64arr_to_periodarr(data, freq, tz)
239 return cls(data, freq=freq)
240
/opt/miniconda3/envs/PY37/lib/python3.7/site-packages/pandas/core/arrays/period.py in dt64arr_to_periodarr(data, freq, tz)
975 data = data._values
976
--> 977 base = freq._period_dtype_code
978 return c_dt64arr_to_periodarr(data.view("i8"), base, tz), freq
979
AttributeError: 'pandas._libs.tslibs.offsets.QuarterBegin' object has no attribute '_period_dtype_code'
有人可以帮助我了解这里发生的事情吗? ps: 这里给出的数据是虚拟数据,不是原始数据
您的语法不正确,请改为尝试:
df["quater_open_dt"] = df["open_dt"].dt.to_period("Q").dt.start_time
print(df)
# Output
open_dt d2 x quater_open_dt
0 2022-01-01 2021-01-03 a 2022-01-01
1 2020-05-01 2021-03-03 b 2020-04-01
2 2023-06-02 2019-04-03 c 2023-04-01