Home > database >  Trying to get 'QS' frequency in pandas for a datetime64[ns] gives attribute error
Trying to get 'QS' frequency in pandas for a datetime64[ns] gives attribute error

Time:04-26

I am working with an external data source and I am trying to get Quaterstart(QS) frequency for a particular data field. I am providing a dummy data and code below.

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')

I am gettinng the following error when I run this

---> 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'

Can someone please help me understand what's happening here? ps: The data given here is dummy data and not the original data

CodePudding user response:

Your syntax is incorrect, try like this instead:

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
  • Related