question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pandas Series: maximum recursion error when replacing empty lists

See original GitHub issue

I have a pandas Series containing lists. I wanted to replace empty lists with NaN. My first approach was using .replace, but that unexpectedly gave me a maximum recursion error.

I am using pandas 0.21.1 on Python 3.4, but other users had the same problem on 0.23

import numpy as np
import pandas as 
ts = pd.Series([[1], [2, 3], [], [4]])
ts.replace([], np.nan)

RuntimeError: maximum recursion depth exceeded in comparison

I achieved the desired output using:

ts[ts.apply(len) == 0] = np.nan

but I don’t understand the maximum recursion error

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
WillAydcommented, Jul 27, 2018

@minggli if it breaks a lot of stuff then your suggestion of handling as an error is probably better. We don’t have first class support for containers within pandas objects, and theoretically won’t until we can get those established as Extension Arrays.

cc @jreback if he has any other thoughts but like I said I’d be OK with error handling for now

0reactions
mingglicommented, Jul 27, 2018

Hi @WillAyd,

Quick question, is list as single element to scalar replacement a supported use case for .replace? the documentation doesn’t include this use case it appears.

Have done some investigation and it appears supporting this use case can break a lot of other use cases. I feel replacing list as one data element in a series or frame is not a supported use case from the look of it.

However, it should not raise recursion error. I think it’s best to handle this issue as error handling.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas Series: maximum recursion error when replacing ...
Since you can pass a list as to_replace parameter in pandas replace function it assumes you are passing in a list of values...
Read more >
What's new in 1.0.0 (January 29, 2020)
When reading code, the contents of an object dtype array is less clear than string . In [9]: pd.Series(['abc', None, 'def'], dtype=pd.
Read more >
Thinking Recursively in Python
Learn how to work with recursion in your Python programs by mastering concepts such as recursive functions and recursive data structures.
Read more >
[Code]-Pandas to pickle error - maximum recursion depth ...
I had the same problem. In my case it was caused by adding a bs4.element.NavigableString element to the dataframe. Converting that to a...
Read more >
Class Notes: Recursion, Part 1
def sumToN(n): if n == 0: return 0 else: return n + sumToN(n-1) print(sumToN(3)) # 6 (works!) print(sumToN(-3)) # RecursionError: maximum recursion depth ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found