Skip to content

Reindex function applied to DataFrame over columns ignores method #14992

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
gcbeltramini opened this issue Dec 26, 2016 · 1 comment
Closed
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@gcbeltramini
Copy link
Contributor

Code Sample

>>> df = pd.DataFrame(data=[[11, 12, 13], [21, 22, 23], [31, 32, 33]],
                      index=[1, 3, 5],
                      columns=[1, 3, 5],
                      dtype=float)
>>> print(df)
      1     3     5
1  11.0  12.0  13.0
3  21.0  22.0  23.0
5  31.0  32.0  33.0

# Applying the reindex function to the columns, with the "ffill" method:
>>> print(df.reindex(columns=range(7), method='ffill'))
    0     1   2     3   4     5   6
1 NaN  11.0 NaN  12.0 NaN  13.0 NaN
3 NaN  21.0 NaN  22.0 NaN  23.0 NaN
5 NaN  31.0 NaN  32.0 NaN  33.0 NaN

# Workaround to get the expected output:
>>> print(df.T.reindex(index=range(7), method='ffill').T)
    0     1     2     3     4     5     6
1 NaN  11.0  11.0  12.0  12.0  13.0  13.0
3 NaN  21.0  21.0  22.0  22.0  23.0  23.0
5 NaN  31.0  31.0  32.0  32.0  33.0  33.0

Problem description

I want to apply the reindex function to the columns using the "ffill" method, but the holes are filled with NaN. The workaround is to transpose the DataFrame, apply the function and then transpose again.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Darwin
OS-release: 16.1.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8

pandas: 0.18.1
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.11.1
scipy: None
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.38.0
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Dec 26, 2016

In [3]: df.reindex(columns=range(7)).ffill(axis=1)
Out[3]: 
    0     1     2     3     4     5     6
1 NaN  11.0  11.0  12.0  12.0  13.0  13.0
3 NaN  21.0  21.0  22.0  22.0  23.0  23.0
5 NaN  31.0  31.0  32.0  32.0  33.0  33.0

is an idiomatic way to do this,

but I see that the method kwarg is not getting propogated to the reindexing methods for columns. Pull requests to fix are welcome!

@jreback jreback added Bug Difficulty Novice Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Dec 26, 2016
@jreback jreback added this to the Next Major Release milestone Dec 26, 2016
@jreback jreback modified the milestones: 0.20.0, Next Major Release Dec 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants