'DataFrameGroupBy' object has no attribute 'set_index'
Question:
I have a dataFrame data. After grouping and resetting the index, I am not able to set my Date column as Index.
data = data.groupby('Payment Date ')
data['Payment Amount '].sum().reset_index()
data = data.set_index('Payment Date ', inplace = True)
data.index
Error:
AttributeError Traceback (most recent call last)
<ipython-input-12-581b0b0bf83f> in <module>
----> 1 data = data.set_index('Payment Date ', inplace = True)
2 data.index
c:usersnitroappdatalocalprogramspythonpython37-32libsite-packagespandascoregroupbygroupby.py in __getattr__(self, attr)
702
703 raise AttributeError(
--> 704 f"'{type(self).__name__}' object has no attribute '{attr}'"
705 )
706
AttributeError: 'DataFrameGroupBy' object has no attribute 'set_index'
Answers:
I think I understood what you wanted to do and what you did not understand (mainly about the way to modifiy objects with pandas). I assume that you wanted to:
- compute your aggregation by payment date in
data
- and then set its index to ‘Payment date’ field
Short answer: if you want to have this result into data
, simply execute:
data = data.groupby('Payment date ')['Payment amount'].sum().to_frame()
‘Payment date ‘ will be your new index, to_frame
prevents your single column resulting dataframe to be squeezed into a pandas Series
(which I think was your first intention to avoid, resetting your index to then set it back).
Let’s dive into your code.
First line
data = data.groupby('Payment Date ')
First line is ok, but might not do exactly what you want. You are taking data
, which I assume is a pandas DataFrame
and reaffect it a pandas DataFrameGroupBy
object. This kind of object does not hold any data, you can see it simply as a mapping between index(s) of your original DataFrame
and associated groups (here, payment dates).
Anyway, you got your groupby object into data
.
Second line
data['Payment Amount '].sum().reset_index()
This line does nothing. It shows the result of the computation in your Jupyter notebook, but nothing has been changed in data
. data
is still the same DataFrameGroupBy
object.
Third line
data = data.set_index('Payment Date ', inplace = True)
An exception is raised, saying that a DataFrameGroupBy
objet has no set_index
method. This is because data
has not been changed by your second line of code.
Even so, I would encourage you to avoid using inplace=True
anytime in your code. You should always go with explicit reassignements.
Your code could look like (if you don’t like the short answer above):
data = data.groupby('Payment date ')
data = data['Payment amount'].sum().reset_index()
data = data.set_index('Payment date ') # No inplace=True!
I have a dataFrame data. After grouping and resetting the index, I am not able to set my Date column as Index.
data = data.groupby('Payment Date ')
data['Payment Amount '].sum().reset_index()
data = data.set_index('Payment Date ', inplace = True)
data.index
Error:
AttributeError Traceback (most recent call last)
<ipython-input-12-581b0b0bf83f> in <module>
----> 1 data = data.set_index('Payment Date ', inplace = True)
2 data.index
c:usersnitroappdatalocalprogramspythonpython37-32libsite-packagespandascoregroupbygroupby.py in __getattr__(self, attr)
702
703 raise AttributeError(
--> 704 f"'{type(self).__name__}' object has no attribute '{attr}'"
705 )
706
AttributeError: 'DataFrameGroupBy' object has no attribute 'set_index'
I think I understood what you wanted to do and what you did not understand (mainly about the way to modifiy objects with pandas). I assume that you wanted to:
- compute your aggregation by payment date in
data
- and then set its index to ‘Payment date’ field
Short answer: if you want to have this result into data
, simply execute:
data = data.groupby('Payment date ')['Payment amount'].sum().to_frame()
‘Payment date ‘ will be your new index, to_frame
prevents your single column resulting dataframe to be squeezed into a pandas Series
(which I think was your first intention to avoid, resetting your index to then set it back).
Let’s dive into your code.
First line
data = data.groupby('Payment Date ')
First line is ok, but might not do exactly what you want. You are taking data
, which I assume is a pandas DataFrame
and reaffect it a pandas DataFrameGroupBy
object. This kind of object does not hold any data, you can see it simply as a mapping between index(s) of your original DataFrame
and associated groups (here, payment dates).
Anyway, you got your groupby object into data
.
Second line
data['Payment Amount '].sum().reset_index()
This line does nothing. It shows the result of the computation in your Jupyter notebook, but nothing has been changed in data
. data
is still the same DataFrameGroupBy
object.
Third line
data = data.set_index('Payment Date ', inplace = True)
An exception is raised, saying that a DataFrameGroupBy
objet has no set_index
method. This is because data
has not been changed by your second line of code.
Even so, I would encourage you to avoid using inplace=True
anytime in your code. You should always go with explicit reassignements.
Your code could look like (if you don’t like the short answer above):
data = data.groupby('Payment date ')
data = data['Payment amount'].sum().reset_index()
data = data.set_index('Payment date ') # No inplace=True!