diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index afe2758db9ab1..24c0dba3d3dc5 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -510,3 +510,5 @@ Bug Fixes - Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`) + +- Bug in ``Timestamp.replace`` where invalid argument caused a ValueError instead of TypeError (:issue:`15240`) \ No newline at end of file diff --git a/pandas/tseries/tests/test_timezones.py b/pandas/tseries/tests/test_timezones.py index db8cda5c76479..f2527bb9648e6 100644 --- a/pandas/tseries/tests/test_timezones.py +++ b/pandas/tseries/tests/test_timezones.py @@ -1195,10 +1195,10 @@ def test_replace(self): expected = Timestamp('2015-02-02 00:05:05.000005005', tz=tz) self.assertEqual(result, expected) - # error + # error GH 15240 return error should be type error not value error def f(): dt.replace(foo=5) - self.assertRaises(ValueError, f) + self.assertRaises(TypeError, f) def f(): dt.replace(hour=0.1) @@ -1208,6 +1208,15 @@ def f(): dt = Timestamp('2013-11-03 01:59:59.999999-0400', tz='US/Eastern') self.assertEqual(dt.tz_localize(None), dt.replace(tzinfo=None)) + # GH 15240 return error should be type error not value error + expected = "TypeError(\"'apple' is an invalid keyword " \ + "argument for this function\",)" + try: + Timestamp("2012").replace(apple=3) + except Exception as e: + result = repr(e) + self.assertEqual(expected, result) + def test_ambiguous_compat(self): # validate that pytz and dateutil are compat for dst # when the transition happens diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 7c65ab5422309..86806b943a9f2 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -712,7 +712,8 @@ class Timestamp(_Timestamp): elif k == 'tzinfo': tzinfo = v else: - raise ValueError("invalid name {} passed".format(k)) + raise TypeError("'{}' is an invalid keyword argument for this function".format(k)) + # ValueError("invalid name {} passed".format(k)) # reconstruct & check bounds value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)