-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Pivot_table drop rows whose entries are all NaN #21969
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
Comments
@noob-saibot : Can you add a reference to the old issue? cc @jreback : Is it intended to drop |
I'm also experiencing this issue in 0.23.4 with an additional snag. If you set dropna=False it will add nonexistent rows to the output table. import pandas as pd
from numpy import nan
data_set = [
{'x': 0, 'y': 0, 'parameter_id': 'a', 'result_value': 3.0},
{'x': 0, 'y': 0, 'parameter_id': 'b', 'result_value': 1.0},
{'x': 0, 'y': 0, 'parameter_id': 'c', 'result_value': 3.0},
{'x': 0, 'y': 0, 'parameter_id': 'd', 'result_value': nan},
{'x': 0, 'y': 1, 'parameter_id': 'a', 'result_value': 1.0},
{'x': 0, 'y': 1, 'parameter_id': 'b', 'result_value': 3.0},
{'x': 0, 'y': 1, 'parameter_id': 'c', 'result_value': nan},
{'x': 0, 'y': 1, 'parameter_id': 'd', 'result_value': nan},
{'x': 0, 'y': 2, 'parameter_id': 'a', 'result_value': 1.0},
{'x': 0, 'y': 2, 'parameter_id': 'b', 'result_value': 3.0},
{'x': 0, 'y': 2, 'parameter_id': 'c', 'result_value': nan},
{'x': 0, 'y': 2, 'parameter_id': 'd', 'result_value': nan},
{'x': 1, 'y': 0, 'parameter_id': 'a', 'result_value': 1.0},
{'x': 1, 'y': 0, 'parameter_id': 'b', 'result_value': 3.0},
{'x': 1, 'y': 0, 'parameter_id': 'c', 'result_value': nan},
{'x': 1, 'y': 0, 'parameter_id': 'd', 'result_value': nan},
{'x': 1, 'y': 1, 'parameter_id': 'a', 'result_value': nan},
{'x': 1, 'y': 1, 'parameter_id': 'b', 'result_value': nan},
{'x': 1, 'y': 1, 'parameter_id': 'c', 'result_value': nan},
{'x': 1, 'y': 1, 'parameter_id': 'd', 'result_value': nan}]
df = pd.DataFrame(data_set)
df.pivot_table(index=['x', 'y'], columns='parameter_id',
values='result_value', dropna=False).reset_index()
Output
Expected Output
|
As of 2.2.2, >>> df = pd.DataFrame({
... 'tract': [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1],
... 'year': [1990, 1990, 2000, 2000, 1990, 1990, 2000, 2000, 1990, 1990, 2000, 2000, 1990],
... 'mode': ['drive', 'walk', 'drive', 'walk', 'drive', 'walk', 'drive', 'walk', 'drive', 'walk', 'drive', 'walk', 'bike'],
... 'count': [np.nan, np.nan, np.nan, 3, 2, np.nan, 1, 4, np.nan, np.nan, 5, 9, np.nan]
... })
>>> df
tract year mode count
0 1 1990 drive NaN
1 1 1990 walk NaN
2 1 2000 drive NaN
3 1 2000 walk 3.0
4 2 1990 drive 2.0
5 2 1990 walk NaN
6 2 2000 drive 1.0
7 2 2000 walk 4.0
8 3 1990 drive NaN
9 3 1990 walk NaN
10 3 2000 drive 5.0
11 3 2000 walk 9.0
12 1 1990 bike NaN
>>> df.pivot_table(index=["tract", "year"], columns="mode", values="count") # dropna=True by default
mode drive walk
tract year
1 2000 NaN 3.0
2 1990 2.0 NaN
2000 1.0 4.0
3 2000 5.0 9.0 Passing >>> df.pivot_table(index=["tract", "year"], columns="mode", values="count", dropna=False)
mode bike drive walk
tract year
1 1990 NaN NaN NaN
2000 NaN NaN 3.0
2 1990 NaN 2.0 NaN
2000 NaN 1.0 4.0
3 1990 NaN NaN NaN
2000 NaN 5.0 9.0 Both of these behaviors seem reasonable to me, but the docs suggest that
It seems like either the behavior or the documentation should change; I would think updating the docs is best, but what say others? |
Uh oh!
There was an error while loading. Please reload this page.
Pivot_table is silently dropping row whose entries fully consisting with NaN. (according to the documentation - dropna : boolean, default True; Do not include columns whose entries are all NaN)
It works fine at version 0.21.1 and 0.22.0.
I found only old bug from 2013.
Expected Output
Output of
pd.show_versions()
pandas: 0.23.1
pytest: None
pip: 9.0.1
setuptools: 28.8.0
Cython: 0.27
numpy: 1.14.5
scipy: None
pyarrow: 0.9.0
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.4.1
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: