Skip to content

Commit 2ac141a

Browse files
committed
fix for issue #15240
replace **kwd argument to explicit argument to raise TypeError when wrong argument name is given
1 parent 3853fe6 commit 2ac141a

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

pandas/tslib.pyx

+27-28
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,9 @@ 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, nanosecond=None,
655+
tzinfo=None)
654656
"""
655657
implements datetime.replace, handles nanoseconds
656658

@@ -675,10 +677,10 @@ class Timestamp(_Timestamp):
675677
_TSObject ts
676678

677679
# set to naive if needed
678-
tzinfo = self.tzinfo
680+
_tzinfo = self.tzinfo
679681
value = self.value
680-
if tzinfo is not None:
681-
value = tz_convert_single(value, 'UTC', tzinfo)
682+
if _tzinfo is not None:
683+
value = tz_convert_single(value, 'UTC', _tzinfo)
682684

683685
# setup components
684686
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
@@ -692,38 +694,35 @@ class Timestamp(_Timestamp):
692694
"{v} for {k}".format(v=type(v), k=k))
693695
return v
694696

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))
697+
if year is not None:
698+
dts.year = validate('year', year)
699+
if month is not None:
700+
dts.month = validate('month', month)
701+
if day is not None:
702+
dts.day = validate('day', day)
703+
if hour is not None:
704+
dts.hour = validate('hour', hour)
705+
if minute is not None:
706+
dts.min = validate('minute', minute)
707+
if second is not None:
708+
dts.sec = validate('second', second)
709+
if microsecond is not None:
710+
dts.us = validate('microsecond', microsecond)
711+
if nanosecond is not None:
712+
dts.ps = validate('nanosecond', nanosecond) * 1000
713+
if tzinfo is not None:
714+
_tzinfo = tzinfo
716715

717716
# reconstruct & check bounds
718717
value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
719718
if value != NPY_NAT:
720719
_check_dts_bounds(&dts)
721720

722721
# set tz if needed
723-
if tzinfo is not None:
724-
value = tz_convert_single(value, tzinfo, 'UTC')
722+
if _tzinfo is not None:
723+
value = tz_convert_single(value, _tzinfo, 'UTC')
725724

726-
result = create_timestamp_from_ts(value, dts, tzinfo, self.freq)
725+
result = create_timestamp_from_ts(value, dts, _tzinfo, self.freq)
727726

728727
return result
729728

0 commit comments

Comments
 (0)