Skip to content

Commit 66d8c41

Browse files
sdementenjreback
authored andcommitted
ERR: Timestamp replace compatibility
closes #15240 Author: Sébastien de Menten <[email protected]> Author: sdementen <[email protected]> Author: Jeff Reback <[email protected]> Closes #15248 from sdementen/timestamp-replace-compatibility and squashes the following commits: e70e75a [Jeff Reback] use tzinfo=object to accept None as a default parameter 52b6c2c [Sébastien de Menten] improve description of whatsnew item 9455ee9 [sdementen] fix for issue #15240 1a40777 [Sébastien de Menten] update what's new with bug fix 5263054 [Sébastien de Menten] fix typos + update doc d5c7b0a [Sébastien de Menten] update exception type raised when wrong argument to Timestamp.replace 2ac141a [sdementen] fix for issue #15240
1 parent 3853fe6 commit 66d8c41

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ Performance Improvements
416416
Bug Fixes
417417
~~~~~~~~~
418418

419+
- Bug in ``Timestamp.replace`` now raises ``TypeError`` when incorrect argument names are given; previously this raised ``ValueError`` (:issue:`15240`)
419420
- Bug in ``Index`` power operations with reversed operands (:issue:`14973`)
420421
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
421422
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)

pandas/tseries/tests/test_timezones.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,6 @@ def test_date_range_span_dst_transition(self):
826826
def test_convert_datetime_list(self):
827827
dr = date_range('2012-06-02', periods=10,
828828
tz=self.tzstr('US/Eastern'), name='foo')
829-
830829
dr2 = DatetimeIndex(list(dr), name='foo')
831830
self.assert_index_equal(dr, dr2)
832831
self.assertEqual(dr.tz, dr2.tz)
@@ -1198,7 +1197,7 @@ def test_replace(self):
11981197
# error
11991198
def f():
12001199
dt.replace(foo=5)
1201-
self.assertRaises(ValueError, f)
1200+
self.assertRaises(TypeError, f)
12021201

12031202
def f():
12041203
dt.replace(hour=0.1)

pandas/tslib.pyx

+39-36
Original file line numberDiff line numberDiff line change
@@ -650,18 +650,25 @@ class Timestamp(_Timestamp):
650650

651651
astimezone = tz_convert
652652

653-
def replace(self, **kwds):
653+
def replace(self, year=None, month=None, day=None,
654+
hour=None, minute=None, second=None, microsecond=None,
655+
nanosecond=None, tzinfo=object, fold=0):
654656
"""
655657
implements datetime.replace, handles nanoseconds
656658
657659
Parameters
658660
----------
659-
kwargs: key-value dict
660-
661-
accepted keywords are:
662-
year, month, day, hour, minute, second, microsecond, nanosecond, tzinfo
663-
664-
values must be integer, or for tzinfo, a tz-convertible
661+
year : int, optional
662+
month : int, optional
663+
day : int, optional
664+
hour : int, optional
665+
minute : int, optional
666+
second : int, optional
667+
microsecond : int, optional
668+
nanosecond: int, optional
669+
tzinfo : tz-convertible, optional
670+
fold : int, optional, default is 0
671+
added in 3.6, NotImplemented
665672
666673
Returns
667674
-------
@@ -671,14 +678,14 @@ class Timestamp(_Timestamp):
671678
cdef:
672679
pandas_datetimestruct dts
673680
int64_t value
674-
object tzinfo, result, k, v
681+
object _tzinfo, result, k, v
675682
_TSObject ts
676683

677684
# set to naive if needed
678-
tzinfo = self.tzinfo
685+
_tzinfo = self.tzinfo
679686
value = self.value
680-
if tzinfo is not None:
681-
value = tz_convert_single(value, 'UTC', tzinfo)
687+
if _tzinfo is not None:
688+
value = tz_convert_single(value, 'UTC', _tzinfo)
682689

683690
# setup components
684691
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
@@ -692,39 +699,35 @@ class Timestamp(_Timestamp):
692699
"{v} for {k}".format(v=type(v), k=k))
693700
return v
694701

695-
for k, v in kwds.items():
696-
if k == 'year':
697-
dts.year = validate(k, v)
698-
elif k == 'month':
699-
dts.month = validate(k, v)
700-
elif k == 'day':
701-
dts.day = validate(k, v)
702-
elif k == 'hour':
703-
dts.hour = validate(k, v)
704-
elif k == 'minute':
705-
dts.min = validate(k, v)
706-
elif k == 'second':
707-
dts.sec = validate(k, v)
708-
elif k == 'microsecond':
709-
dts.us = validate(k, v)
710-
elif k == 'nanosecond':
711-
dts.ps = validate(k, v) * 1000
712-
elif k == 'tzinfo':
713-
tzinfo = v
714-
else:
715-
raise ValueError("invalid name {} passed".format(k))
702+
if year is not None:
703+
dts.year = validate('year', year)
704+
if month is not None:
705+
dts.month = validate('month', month)
706+
if day is not None:
707+
dts.day = validate('day', day)
708+
if hour is not None:
709+
dts.hour = validate('hour', hour)
710+
if minute is not None:
711+
dts.min = validate('minute', minute)
712+
if second is not None:
713+
dts.sec = validate('second', second)
714+
if microsecond is not None:
715+
dts.us = validate('microsecond', microsecond)
716+
if nanosecond is not None:
717+
dts.ps = validate('nanosecond', nanosecond) * 1000
718+
if tzinfo is not object:
719+
_tzinfo = tzinfo
716720

717721
# reconstruct & check bounds
718722
value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
719723
if value != NPY_NAT:
720724
_check_dts_bounds(&dts)
721725

722726
# set tz if needed
723-
if tzinfo is not None:
724-
value = tz_convert_single(value, tzinfo, 'UTC')
725-
726-
result = create_timestamp_from_ts(value, dts, tzinfo, self.freq)
727+
if _tzinfo is not None:
728+
value = tz_convert_single(value, _tzinfo, 'UTC')
727729

730+
result = create_timestamp_from_ts(value, dts, _tzinfo, self.freq)
728731
return result
729732

730733
def isoformat(self, sep='T'):

0 commit comments

Comments
 (0)