Forums

Python - Pandas: Getting a TypeError when running pivot_table()

Hello all, I am trying to do a simple .pivot_table() operation on a dataframe using pandas and I keep getting this error:

TypeError: int() argument must be a string, a bytes-like object or a number, not '_NoValueType'

When running the .py file locally I get no such error, only on pythonanywhere.

I have to say that my research has not returned any results

I am running this snippet

df.pivot_table(index=index,values=values',columns=columns',aggfunc=np.sum,fill_value=0)

index, values, and columns are all strings that refer to the columns of df.

Thank you all

Here's the entire error message I get

    Traceback (most recent call last):
  File "/home/ddjpageant/examination.py", line 260, in <module>
    freq_2020.pivot_table(index='month',values='number',columns='main_new_status',aggfunc=np.sum,fill_value=0)
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 7028, in pivot_table
    return pivot_table(
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/reshape/pivot.py", line 162, in pivot_table
    table = table.sort_index(axis=1)
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 5579, in sort_index
    return super().sort_index(
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/generic.py", line 4534, in sort_index
    indexer = get_indexer_indexer(
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/sorting.py", line 77, in get_indexer_indexer
    target._get_codes_for_sorting(), orders=ascending, na_position=na_position
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/indexes/multi.py", line 2327, in _get_codes_for_sorting
    return [
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/indexes/multi.py", line 2328, in <listcomp>
    Categorical.from_codes(level_codes, cats(level_codes), ordered=True)
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 589, in from_codes
    if len(codes) and (codes.max() >= len(dtype.categories) or codes.min() < -1):
  File "/home/ddjpageant/.local/lib/python3.8/site-packages/numpy/core/_methods.py", line 43, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial, where)
TypeError: int() argument must be a string, a bytes-like object or a number, not '_NoValueType'

You have values in your table that you're trying to treat as integers when they are the _NoValueType. Check the data that you're working on for entries with no data and then you can possibly trace back from there to where the values came from and work out how that got into your table.

That makes some sense, but when I run it locally on python I get no errors. It is a simple table like this:

    month | status | number
       1       Completed      44
       1       Potential      14
       1             New       7
       1    Discontinued       1
       2       Completed      40

and I do a simple pivot_table which even when using a fill_value=0 in the pivot_table() I get this error.

My hunch is that there is some version miss-match in pythonanywhere? Some package that relates to pivot_tables is missing?

The solution came from upgrading numpy.

As simple as pip install --upgrade numpy --user

Feel free to mark this as resolved, or delete it, or keep it there in case anyone else gets this error

Glad you solved it -- thanks for letting us know, we'll keep it in case someone encounters similar behavior.

Sharing. I had the same issue, tried your solution but did not work. Eventually what worked for me was to downgrade my version of numpy.

@gd2244 What was the failing version and what was the working one?

1.20.3 down to 1.20.1

Thanks for sharing your solution, @gd2244.