Pandas Series: maximum recursion error when replacing empty lists
See original GitHub issueI 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:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
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.