Skip to content
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

Melting with not present column does not produce error #23570

Closed
michaelsilverstein opened this issue Nov 8, 2018 · 4 comments · Fixed by #23575
Closed

Melting with not present column does not produce error #23570

michaelsilverstein opened this issue Nov 8, 2018 · 4 comments · Fixed by #23575
Labels
Error Reporting Incorrect or improved errors from pandas good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@michaelsilverstein
Copy link
Contributor

michaelsilverstein commented Nov 8, 2018

Code Sample

import pandas as pd
import numpy as np
# Generate data
people = ['Susie', 'Alejandro']
day = ['Monday', 'Tuesday', 'Wednesday']
data = [[person, d, *np.random.randint(0, 5, 2)]  for person in people for d in day]
df = pd.DataFrame(data, columns=['Name', 'day', 'burgers', 'fries'])
df.head()
Name day burgers fries
Susie Monday 4 0
Susie Tuesday 0 1
Susie Wednesday 0 1
Alejandro Monday 4 2
Alejandro Tuesday 2 0
# Melt on column that's not present in `df`
df.melt(['Name', 'day'], ['Burgers', 'fries'])

Outputs warning:

/home/ubuntu/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:1472: FutureWarning: 
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

See the documentation here:
https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
  return self._getitem_tuple(key)

Problem description

This behavior should produce an error. Warnings aren't always taken seriously and if this melting operation were apart of a larger chain of operations, it would be unclear what the cause of the warning was since the warning does not directly address the actual problem. There is no scenario in which support for melting on non-present columns would be beneficial and there are clear reasons why it would be beneficial to alert users if they've made this mistake.

Expected Output

df.melt(['Name', 'day'], ['Burgers', 'fries']) should be treated like df.melt(['Name', 'Day']) (where Day is not present in the df) and produce a Traceback.

@TomAugspurger
Copy link
Contributor

That warning is coming indirectly via a .loc. I'm not sure when it's slated to be enforced.

Regardless, we could add a check to melt that all the names are valid. Interested in submitting a PR?

@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Nov 8, 2018
@TomAugspurger TomAugspurger added Reshaping Concat, Merge/Join, Stack/Unstack, Explode good first issue Error Reporting Incorrect or improved errors from pandas Effort Low labels Nov 8, 2018
@TomAugspurger
Copy link
Contributor

I think the check needs to happen roughly before

frame = frame.loc[:, id_vars + value_vars]

@michaelsilverstein
Copy link
Contributor Author

Just made the PR, I'm having some trouble running pandas from the source directory, I get this error:

ImportError: C extension: No module named 'pandas._libs.tslibs.conversion' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

But the setup failed somewhere along the way. Any suggestions for testing?

@TomAugspurger
Copy link
Contributor

You need to build the C extensions: http://pandas-docs.github.io/pandas-docs-travis/contributing.html

@jreback jreback modified the milestones: Contributions Welcome, 0.24.0 Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants