Index.putmask with an invalid mask causes a crash due to stack overflow · Issue #18368 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

Seems to happen regardless of the type of index, and for any type of invalid mask.

mask with too few elements:

In [2]: idx = pd.Index(list('abca'))

In [3]: idx.putmask([True, True, False], 'x') Fatal Python error: Cannot recover from stack overflow.

mask with too many elements:

In [2]: idx = pd.CategoricalIndex(list('abca'))

In [3]: idx.putmask(np.array([True, False]*3), 'c') Fatal Python error: Cannot recover from stack overflow.

Entirely wrong mask:

In [2]: idx = pd.period_range('2017Q1', periods=4, freq='Q')

In [3]: idx.putmask('foo', 'bar') Fatal Python error: Cannot recover from stack overflow.

Doesn't appear to be an issue with np.putmask:

In [2]: a = np.arange(5)

In [3]: np.putmask(a, [True, False], -1)

ValueError: putmask: mask and data must be the same size

Problem description

An invalid mask in Index.putmask causes a stack overflow.

Expected Output

I'd expect an exception to be raised.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: b00e62c
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0.dev0+159.gb00e62c
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.13.1
scipy: 0.19.1
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 6.1.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.2
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.0
pandas_gbq: None
pandas_datareader: None