Skip to content

NDFrame.interpolate(): variable 'ax' not assigned when axis='index' #29132

@ellequelle

Description

@ellequelle

The NDFrame.interpolate function fails when passing a string as axis. Example:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame(np.zeros((3,2)), columns=['a','b'])
>>> df.iloc[1] = np.nan
>>> df
     a    b
0  0.0  0.0
1  NaN  NaN
2  0.0  0.0
>>> df.interpolate(axis=0)
     a    b
0  0.0  0.0
1  0.0  0.0
2  0.0  0.0
>>> df.interpolate(axis='index')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lehanson/anaconda3/envs/plots/lib/python3.7/site-packages/pandas/core/generic.py", line 7006, in interpolate
    ax = _maybe_transposed_self._get_axis_number(ax)
UnboundLocalError: local variable 'ax' referenced before assignment

From the documentation and from the function itself, it looks like df.interpolate(axis='index') was intended to work, but that maybe someone accidentally deleted a line in generic.py? The function seems to work properly if I add ax = axis in the else block here:

pandas/pandas/core/generic.py

Lines 6998 to 7006 in 171c716

if axis == 0:
ax = self._info_axis_name
_maybe_transposed_self = self
elif axis == 1:
_maybe_transposed_self = self.T
ax = 1
else:
_maybe_transposed_self = self
ax = _maybe_transposed_self._get_axis_number(ax)

I am using pandas version 0.25.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions