On checking I found my polars version :

pl.__version__

0.17.3

https://pola-rs.github.io/polars/py-polars/html/reference/dataframe/api/polars.DataFrame.groupby.html

I need to do:

df.groupby("a").agg(pl.col("b").sum())  # there is no underscore in groupby

#output

shape: (3, 2)
a   b
str i64
"a" 2
"c" 3
"b" 5

and the document says :

Deprecated since version 0.19.0: This method has been renamed to DataFrame.group_by().

This is the new document for polars version 0.19

https://pola-rs.github.io/polars/py-polars/html/reference/dataframe/api/polars.DataFrame.group_by.html#polars-dataframe-group-by

Answer from Talha Tayyab on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › pandas attributeerror: 'dataframe' object has no attribute 'group_by'
r/learnpython on Reddit: Pandas AttributeError: 'DataFrame' object has no attribute 'group_by'
February 28, 2018 -

Hello,

Has anyone ever come across this before?

I'm trying to group some data in a dataframe and getting this error. The steps I've taken are:

  1. in a for loop:

read in a csv from an api using pd.read_csv() replaced some values in a column using a for loop and .loc[] appended the resulting data frame to a list

2) concatenated the list of dataframes using pd.concat()

3) added a calculated column to the new DF by multiplying another column

4) added two empty columns

5) filtered the DF using .loc[] based on a value within a column

6) filtered the DF using .loc[] based on a value in a different column

7) tried to use this code:

new_DF = old_df.group_by(['col1', 'col_2', 'col_3', 'adgroup', 'col_4', 'col5', 'col6'], as_index=False)[['col7', 'col8', 
'col9']].sum()

The DF seems to behaving normally for example I can do dtypes and columns on it and add columns which are calculated from other columns. What is super frustrating is that I can do pd.to_csv() and then pd.read_csv() on the DF and then I'm able to do the grouping I want (however this isn't ideal which is why I'm posting).

Any advice would be appreciated.

Cheers

Discussions

How to get group from groupby by key? - Python - Data Science Dojo Discussions
Hello, I am having trouble accessing a specific group from my pandas DataFrame using the groupby() method. I have grouped my DataFrame by the ‘A’ column, and now I am trying to access the group with the key ‘foo’. I have tried using the .get_group() method, but it returns the group ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
4
0
February 24, 2023
python - AttributeError: 'DataFrame' object has no attribute 'group_by' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
python - AttributeError: 'DataFrame' object has no attribute 'group' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
python - Error in groupby pandas - Stack Overflow
I am running a script in windows and I get the following error: Traceback (most recent call last): File "C:\Users\esalazar\Desktop\datos\stat_cea_2011\emas\amealco\promedios-emas.py", line... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › pandas-dev › pandas › issues › 11640
BUG AttributeError: 'DataFrameGroupBy' object has no attribute '_obj_with_exclusions' · Issue #11640 · pandas-dev/pandas
November 18, 2015 - In [5]: df.groupby('a').mean() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-29-a830c6135818> in <module>() ----> 1 df.groupby('a').mean() /home/nicolas/Git/pandas/pandas/core/groupby.py in mean(self) 764 self._set_selection_from_grouper() 765 f = lambda x: x.mean(axis=self.axis) --> 766 return self._python_agg_general(f) 767 768 def median(self): /home/nicolas/Git/pandas/pandas/core/groupby.py in _python_agg_general(self, func, *args, **kwargs) 1245 output[name] = self._try_cast(values[mask], result)
Author   nbonnotte
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to get group from groupby by key? - Python - Data Science Dojo Discussions
February 24, 2023 - Hello, I am having trouble accessing a specific group from my pandas DataFrame using the groupby() method. I have grouped my DataFrame by the ‘A’ column, and now I am trying to access the group with the key ‘foo’. I have tried using the .get_group() method, but it returns the group ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.1 › reference › groupby.html
GroupBy — pandas 1.1.5 documentation
GroupBy objects are returned by groupby calls: pandas.DataFrame.groupby(), pandas.Series.groupby(), etc · Dict {group name -> group labels}
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-dataframegroupby-object-to-dataframe-in-pandas
How to Convert DataFrameGroupBy Object to DataFrame in Pandas | Saturn Cloud Blog
January 20, 2024 - However, the resulting object is ... may not be suitable for further analysis. In this blog post, we will show you how to convert a DataFrameGroupBy object to a regular DataFrame object in Pandas. ... Before we dive into the conversion process, let’s first understand what a DataFrameGroupBy object is. When you apply the groupby function on a DataFrame object, Pandas returns a DataFrameGroupBy object. This object has grouped the ...
🌐
Kaggle
kaggle.com › questions-and-answers › 511815
Convert DataFrameGroupBy object to a DataFrame
Click here if you are not automatically redirected after 5 seconds.
Find elsewhere
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › issue-with-pyspark-groupby-groupeddata › td-p › 7255
Issue with Pyspark GroupBy GroupedData - Databricks Community - 7255
March 27, 2023 - I tried the below approach, But ... anyother approach on doing this? ... The issue with your code is that the groupBy operation returns a GroupedData object, which does not have a get_group method....
Top answer
1 of 1
5

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:

  1. compute your aggregation by payment date in data
  2. 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!
🌐
GitHub
github.com › pandas-dev › pandas › issues › 11562
'mode' not recognized by df.groupby().agg(), but pd.Series.mode works · Issue #11562 · pandas-dev/pandas
November 9, 2015 - df.groupby('B').agg('mode') ... AttributeError: Cannot access callable attribute 'mode' of 'DataFrameGroupBy' objects, try using the 'apply' method
Author   patricksurry
🌐
GitHub
github.com › pandas-dev › pandas › issues › 43564
ENH: DataFrameGroupby.value_counts · Issue #43564 · pandas-dev/pandas
September 14, 2021 - import pandas as pd df = pd.DataFrame({'gender': ['female', 'male', 'female', 'male', 'female', 'male'], 'education': ['low', 'medium', 'high', 'low', 'high', 'low'], 'country': ['US', 'FR', 'US', 'FR', 'FR', 'US']}) # These all work fine: # value_counts() on a DataFrame df[['gender', 'education']].value_counts(normalize=True) df['gender'].value_counts(normalize=True) # on a Series df.groupby('country')['gender'].value_counts(normalize=True) # on a SeriesGroupby # but fails on DataFrameGroupBy df.groupby('country')[['gender', 'education']].value_counts(normalize=True) -------------------------
Author   corriebar
🌐
Google Groups
groups.google.com › g › pydata › c › 4ZjOP0Lfjdc
Problem with groupby and nth in pandas 0.18.1
July 5, 2016 - I noticed that you can also have the original behaviour of 0.17 by passing as_index=False: In [13]: df.groupby('device', as_index=False)['timestamp'].nth(0) Out[13]: 0 0 3 1 Name: timestamp, dtype: int64 Are you sure the transform('idxmin') works? I get an error when I try that (both on 0.17.1 as 0.18.1): AttributeError: 'SeriesGroupBy' object has no attribute 'idxmim' Whoops, there was a typo in your code, which is the cause that it failed: idxmim of course does not work, but idxmin does :-)  ·
🌐
GitHub
github.com › dask › dask › issues › 3438
Dataframe groupby()[column].agg fails with AttributeError · Issue #3438 · dask/dask
April 24, 2018 - AttributeError Traceback (most recent call last) <ipython-input-1-b43466d1ae6a> in <module>() 6 -0.666]}) 7 ddf = dd.from_pandas(df, npartitions=1) ----> 8 ddf.groupby('A')['B'].agg('var') 9 10 ~/applications/anaconda3/lib/python3.6/site-packages/dask/dataframe/groupby.py in agg(self, arg, split_every, split_out) 1217 @derived_from(pd.core.groupby.SeriesGroupBy) 1218 def agg(self, arg, split_every=None, split_out=1): -> 1219 return self.aggregate(arg, split_every=split_every, split_out=split_out) ~/applications/anaconda3/lib/python3.6/site-packages/dask/dataframe/groupby.py in aggregate(self, arg, split_every, split_out) 1211 1212 if not isinstance(arg, (list, dict)): -> 1213 result = result[result.columns[0]] 1214 1215 return result AttributeError: 'Series' object has no attribute 'columns' No one assigned ·
Author   joergdietrich