Python Forum
GroupBy - Sum = Error [datetime64 type does not support sum operations] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: GroupBy - Sum = Error [datetime64 type does not support sum operations] (/thread-40994.html)



GroupBy - Sum = Error [datetime64 type does not support sum operations] - BSDevo - Oct-26-2023

Hi,
Im trying to solve this particular issue.
Im using two columns to display Price and Market.
        lftcolumn, rghtcolumn = st.columns((9, 1))
        with lftcolumn:
            pme_df = filtered_df.groupby(by = ["Market"], as_index = False)["Price"].sum().sort_values(by= ["Price"], ascending=False).reset_index()
            pmefig = px.bar(pme_df, x = "Market", y = "Price", text = ['${:,.2f}'.format(x) for x in pme_df["Price"]],template = "seaborn") 
            pmefig.update_layout(xaxis_title="Market",yaxis_title="Price")
            st.plotly_chart(pmefig,use_container_width=True, height = 200)
        with rghtcolumn:
            # # st.subheader("Percentage")
            st.subheader(' "%" dif')
            procentai = pme_df['Price']
  
            st.write(procentai.pct_change() * 100)
Which is fine - it works but with % column i have numbers instead of Market as index.
Im trying to make Index for this as Market.

            pme_df = filtered_df[["Market", "Price"]]
            pme_df = filtered_df.set_index(["Market", "Price"], inplace=True)
            pme_df = filtered_df.groupby(["Market", "Price"]).sum()
Error:
TypeError: datetime64 type does not support sum operations
            pme_df = filtered_df[["Market", "Price"]]
            pme_df = filtered_df.set_index(["Market", "Price"], inplace=True)
            pme_df = filtered_df.groupby(["Market"])["Price"].sum()
Error:
KeyError: 'Column not found: Price'
            pme_df = filtered_df[["Market", "Price"]]
            pme_df = filtered_df.set_index(["Market", "Price"], inplace=True)
            pme_df = filtered_df.groupby(["Market"]).sum()
Error:
TypeError: datetime64 type does not support sum operations
Market contains market areas( string/object type column) and price ( float)
filtered_df - various columns including date column
Whats causing this issue for me ?
Thank You.


RE: GroupBy - Sum = Error [datetime64 type does not support sum operations] - deanhystad - Oct-26-2023

This does not change filtered_df
pme_df = filtered_df[["Market", "Price"]]
pme_df is a dataframe that only has columns Market and Price, but you throw pme_df away on the next line when you do this:
pme_df = filtered_df.set_index(["Market", "Price"], inplace=True)
This changes filtered_df, which still has all it's columns at this time. pme_df is set to None because that is what gets returned when you change a dataframe inplace.

When you get around to doing this:
pme_df = filtered_df.groupby(["Market"])["Price"].sum()
you are still working off filtered_df, which has all it's original columns except Market and Price, which have been converted to indices.

Is this what you wanted to do?
pme_df = filtered_df.groupby("Market")["Price"].sum()



RE: GroupBy - Sum = Error [datetime64 type does not support sum operations] - BSDevo - Oct-27-2023

(Oct-26-2023, 07:22 PM)deanhystad Wrote: 1

    pme_df = filtered_df.groupby(by = ["Market"], as_index = False)["Price"].sum().sort_values(by= ["Price"], ascending=False).reset_index()
This is what it is, but im trying to make Market as an index.
My current table shows:
0 None
1 -6.56
2 - 5.9

So i want to look like this:
Atlanta None
Laredo -6.56
Ontario -5.9

Thats why i trying to make my Market as Index.


RE: GroupBy - Sum = Error [datetime64 type does not support sum operations] - deanhystad - Oct-27-2023

Fine, make it an index. The problem was that you were not doing so correctly.
pme_df = filtered_df[["Market", "Price"]].set_index("Market").sum()



RE: GroupBy - Sum = Error [datetime64 type does not support sum operations] - BSDevo - Oct-27-2023

(Oct-27-2023, 05:04 PM)deanhystad Wrote: Fine, make it an index. The problem was that you were not doing so correctly.
pme_df = filtered_df[["Market", "Price"]].set_index("Market").sum()

Thank You - worked like a charm !
I see now where i did a mistake.