-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Missing values in ordered category breaks sorting of unstacked columns #28597
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
Workaround: call |
Fixed in versions >=1.0.0 |
hmm so we have a test for this? can you push a PR to add one? |
I've never contributed a test before, but will have a go. |
@mojones you still interesting in working on this? See our contributing guide here: https://pandas.pydata.org/docs/development/contributing.html |
I'll take a stab at it today. Is there a more appropriate place than this comment thread to ask questions about contributing the test? |
c5a1f9e is the first new commit
|
xref #17605 (comment) |
xref #23865 (similar issue) |
Code Sample, a copy-pastable example if possible
Problem description
I have a dataframe with an ordered category column
foo
. I want to group by both columns then take the size of the groups and unstack to get a summary table. If all of the values in my ordered category are in the data, then the result is as expected:My columns appear in the specified order. However, if for some reason I have categories that are listed but don't actually appear in the data (in this case,
'tiny'
) the order seems to be determined by the order that the categories appear in the series before stacking:I originally encountered this when using
pd.cut
to group rows into bins, but an explicitly ordered category I thought made a clearer example. It's also very easy to end up in this situation when filtering a large dataframe.Expected Output
Output of
pd.show_versions()
[paste the output of
pd.show_versions()
here below this line]INSTALLED VERSIONS
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-64-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
pandas: 0.24.2
pytest: None
pip: 19.1.1
setuptools: 41.0.1
Cython: None
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.0
openpyxl: None
xlrd: 1.2.0
xlwt: None
xlsxwriter: None
lxml.etree: 4.3.3
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: