Python Forum
GroupBy - Sum = Error [datetime64 type does not support sum operations]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GroupBy - Sum = Error [datetime64 type does not support sum operations]
#1
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.
Reply
#2
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()
Reply
#3
(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.
Reply
#4
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()
BSDevo likes this post
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Bitwise Operations in numpy Sowmya 3 281 Apr-03-2024, 02:51 PM
Last Post: deanhystad
  [Numpy] Load date/time from .txt to 'datetime64' type. water 4 631 Mar-01-2024, 11:16 PM
Last Post: Gribouillis
  Type error in Cross_val_score Vicral 0 1,840 Jul-20-2021, 12:18 PM
Last Post: Vicral
  groupby on var with missing values error zenvega 0 1,766 May-07-2021, 07:40 PM
Last Post: zenvega
  type error array BrianPA 2 2,428 Jan-17-2021, 01:48 PM
Last Post: BrianPA
  How to fill datetime64 field in numpy structured array? AlekseyPython 0 2,293 Oct-20-2020, 08:17 AM
Last Post: AlekseyPython
  Error binding parameter 0 - probably unsupported type. illmattic 7 10,399 Jul-18-2020, 09:32 PM
Last Post: illmattic
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 19,011 May-22-2019, 02:31 PM
Last Post: twinpiques
  Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' mcgrim 3 18,372 Mar-22-2019, 01:29 PM
Last Post: buran
  Type Error Confusion Oliver 4 14,506 Dec-06-2017, 03:20 PM
Last Post: Oliver

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020