Skip to content

QST: Pandas Crosstab dos not support Float (with capital F) number formats #50313

Closed
@frbelotto

Description

@frbelotto

Research

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/74830329/pandas-crosstab-dos-not-support-float-with-capital-f-number-formats

Question about pandas

Hello guys,
I´ve noted that pandas crosstab does not support Float formats (instead of float). What I really would like is to understand why and if the output error message could be clearer to understand?
Best regards.

I am working on a sample data transaction dataframe. Such base contains cliente ID, transaction gross value (GMV) and revenue. Take this example as DF :

df = pd.DataFrame({
    'id' :  [1,2,3,4,5],
    'date' : [datetime(2022,6,1),datetime(2022,12,31),datetime(2022,12,31),datetime(2022,12,31),datetime(2022,12,31)],
    'revenue' : [10.1, 10.1, 10.1, 10.1, 10.1]})
df = df.astype({'revenue':'Float64'})

Now I create a crosstab

CrossTab = pd.crosstab(df['id'], df['date'], values=df['revenue'], rownames=None, colnames=None, aggfunc='sum', margins=True, margins_name='All', dropna=False, normalize=False)

the output

Output exceeds the [size limit](command:workbench.action.openSettings?[). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?62540584-615b-4486-a364-032ed486e331)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 CrossTab = pd.crosstab(df['id'], df['date'], values=df['revenue'], rownames=None, colnames=None, aggfunc='sum', margins=True, margins_name='All', dropna=False, normalize=False)

File c:\Users\fabio\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\reshape\pivot.py:691, in crosstab(index, columns, values, rownames, colnames, aggfunc, margins, margins_name, dropna, normalize)
    688     df["__dummy__"] = values
    689     kwargs = {"aggfunc": aggfunc}
--> 691 table = df.pivot_table(
    692     "__dummy__",
    693     index=unique_rownames,
    694     columns=unique_colnames,
    695     margins=margins,
    696     margins_name=margins_name,
    697     dropna=dropna,
    698     **kwargs,
    699 )
    701 # Post-process
    702 if normalize is not False:

File c:\Users\fabio\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py:8728, in DataFrame.pivot_table(self, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed, sort)
   8711 @Substitution("")
   8712 @Appender(_shared_docs["pivot_table"])
   8713 def pivot_table(
   (...)
...
--> 292     raise TypeError(dtype)  # pragma: no cover
    294 converted = maybe_downcast_numeric(result, dtype, do_round)
    295 if converted is not result:

TypeError: Float64

Metadata

Metadata

Assignees

Labels

NA - MaskedArraysRelated to pd.NA and nullable extension arraysNeeds TestsUnit test(s) needed to prevent regressionsReshapingConcat, Merge/Join, Stack/Unstack, Explodegood first issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions