Skip to content

using DataFrame.resample with 'agg' method on non-existant columns provides unexpected behavior #19552

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

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ Groupby/Resample/Rolling
- Bug in :func:`DataFrame.groupby` where aggregation by ``first``/``last``/``min``/``max`` was causing timestamps to lose precision (:issue:`19526`)
- Bug in :func:`DataFrame.transform` where particular aggregation functions were being incorrectly cast to match the dtype(s) of the grouped data (:issue:`19200`)
- Bug in :func:`DataFrame.groupby` passing the `on=` kwarg, and subsequently using ``.apply()`` (:issue:`17813`)
- Bug in :func:`DataFrame.resample().aggregate` not raising a `ValueError` when aggregating a non-existent column (:issue:`16766`)

Sparse
^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ def nested_renaming_depr(level=4):

elif isinstance(obj, ABCSeries):
nested_renaming_depr()
elif isinstance(obj, ABCDataFrame) and \
k not in obj.columns:
raise ValueError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actuallly I think this should be a KeyError. can you do another PR to fix?

"Column '{col}' does not exist!".format(col=k))

arg = new_arg

Expand Down
18 changes: 16 additions & 2 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from pandas.core.dtypes.generic import ABCSeries, ABCDataFrame
from pandas.compat import range, lrange, zip, product, OrderedDict
from pandas.core.base import SpecificationError
from pandas.errors import UnsupportedFunctionCall
from pandas.core.groupby import DataError
import pandas.core.common as com
Expand Down Expand Up @@ -614,7 +613,7 @@ def f():
t[['A']].agg({'A': ['sum', 'std'],
'B': ['mean', 'std']})

pytest.raises(SpecificationError, f)
pytest.raises(ValueError, f)

def test_agg_nested_dicts(self):

Expand Down Expand Up @@ -659,6 +658,21 @@ def f():
'B': {'rb': ['mean', 'std']}})
assert_frame_equal(result, expected, check_like=True)

def test_try_aggregate_non_existing_column(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this test is needed because test_agg_misc test covers this case. I added it to show reviewer that original issue is addressed. Let me know and I'll remove this new test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to keep it.

# GH 16766
data = [
{'dt': datetime(2017, 6, 1, 0), 'x': 1.0, 'y': 2.0},
{'dt': datetime(2017, 6, 1, 1), 'x': 2.0, 'y': 2.0},
{'dt': datetime(2017, 6, 1, 2), 'x': 3.0, 'y': 1.5}
]
df = DataFrame(data).set_index('dt')

# Error as we don't have 'z' column
with pytest.raises(ValueError):
df.resample('30T').agg({'x': ['mean'],
'y': ['median'],
'z': ['sum']})

def test_selection_api_validation(self):
# GH 13500
index = date_range(datetime(2005, 1, 1),
Expand Down