Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ellequelle opened this issue Oct 21, 2019 · 4 comments · Fixed by #29142
Closed

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

ellequelle opened this issue Oct 21, 2019 · 4 comments · Fixed by #29142
Labels
good first issue Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@ellequelle
Copy link
Contributor

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

@TomAugspurger
Copy link
Contributor

Thanks for the report, confirmed on master.

We should have an axis = self._get_axis_number(axis) at the start of NDFrame.interpolate (and a whatsnew note & test to ensure we don't regress).

@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Oct 21, 2019
@TomAugspurger TomAugspurger added good first issue Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Oct 21, 2019
@ellequelle
Copy link
Contributor Author

ellequelle commented Oct 21, 2019

@TomAugspurger do you mean something like this? Also: there doesn't seem to be a whatsnew file since 0.25.2, does this go into v0.25.3.rst?

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

Edit: sorry @gabriellm1 I didn't see your comment!

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Oct 21, 2019 via email

ellequelle added a commit to ellequelle/pandas that referenced this issue Oct 21, 2019
NDFrame.interpolate fails if axis is specified by name and not integer
@coderkalyan
Copy link

Hi, I'm new to pandas development (but a fan). Can I try this as part of #hacktoberfest, and where do I start?

@jreback jreback modified the milestones: Contributions Welcome, 1.0 Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants