Skip to content

Commit bba384d

Browse files
author
Matt Roeschke
committed
fix timestamp
1 parent 0bc93b0 commit bba384d

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

pandas/_libs/tslibs/nattype.pyx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ class NaTType(_NaT):
478478
Parameters
479479
----------
480480
freq : a freq string indicating the rounding resolution
481+
ambiguous : bool, 'NaT', default 'raise'
482+
- bool contains flags to determine if time is dst or not (note
483+
that this flag is only applicable for ambiguous fall dst dates)
484+
- 'NaT' will return NaT for an ambiguous time
485+
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
481486
482487
Raises
483488
------
@@ -490,6 +495,15 @@ class NaTType(_NaT):
490495
Parameters
491496
----------
492497
freq : a freq string indicating the flooring resolution
498+
ambiguous : bool, 'NaT', default 'raise'
499+
- bool contains flags to determine if time is dst or not (note
500+
that this flag is only applicable for ambiguous fall dst dates)
501+
- 'NaT' will return NaT for an ambiguous time
502+
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
503+
504+
Raises
505+
------
506+
ValueError if the freq cannot be converted
493507
""")
494508
ceil = _make_nat_func('ceil', # noqa:E128
495509
"""
@@ -498,6 +512,15 @@ class NaTType(_NaT):
498512
Parameters
499513
----------
500514
freq : a freq string indicating the ceiling resolution
515+
ambiguous : bool, 'NaT', default 'raise'
516+
- bool contains flags to determine if time is dst or not (note
517+
that this flag is only applicable for ambiguous fall dst dates)
518+
- 'NaT' will return NaT for an ambiguous time
519+
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
520+
521+
Raises
522+
------
523+
ValueError if the freq cannot be converted
501524
""")
502525

503526
tz_convert = _make_nat_func('tz_convert', # noqa:E128

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ class Timestamp(_Timestamp):
656656

657657
return create_timestamp_from_ts(ts.value, ts.dts, ts.tzinfo, freq)
658658

659-
def _round(self, freq, rounder):
659+
def _round(self, freq, rounder, ambiguous='raise'):
660660
if self.tz is not None:
661661
value = self.tz_localize(None).value
662662
else:
@@ -668,10 +668,10 @@ class Timestamp(_Timestamp):
668668
r = round_ns(value, rounder, freq)[0]
669669
result = Timestamp(r, unit='ns')
670670
if self.tz is not None:
671-
result = result.tz_localize(self.tz)
671+
result = result.tz_localize(self.tz, ambiguous=ambiguous)
672672
return result
673673

674-
def round(self, freq):
674+
def round(self, freq, ambiguous='raise'):
675675
"""
676676
Round the Timestamp to the specified resolution
677677
@@ -682,32 +682,55 @@ class Timestamp(_Timestamp):
682682
Parameters
683683
----------
684684
freq : a freq string indicating the rounding resolution
685+
ambiguous : bool, 'NaT', default 'raise'
686+
- bool contains flags to determine if time is dst or not (note
687+
that this flag is only applicable for ambiguous fall dst dates)
688+
- 'NaT' will return NaT for an ambiguous time
689+
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
685690
686691
Raises
687692
------
688693
ValueError if the freq cannot be converted
689694
"""
690-
return self._round(freq, np.round)
695+
return self._round(freq, np.round, ambiguous)
691696

692-
def floor(self, freq):
697+
def floor(self, freq, ambiguous='raise'):
693698
"""
694699
return a new Timestamp floored to this resolution
695700
696701
Parameters
697702
----------
698703
freq : a freq string indicating the flooring resolution
704+
ambiguous : bool, 'NaT', default 'raise'
705+
- bool contains flags to determine if time is dst or not (note
706+
that this flag is only applicable for ambiguous fall dst dates)
707+
- 'NaT' will return NaT for an ambiguous time
708+
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
709+
710+
Raises
711+
------
712+
ValueError if the freq cannot be converted
699713
"""
700-
return self._round(freq, np.floor)
714+
return self._round(freq, np.floor, ambiguous)
701715

702-
def ceil(self, freq):
716+
def ceil(self, freq, ambiguous='raise'):
703717
"""
704718
return a new Timestamp ceiled to this resolution
705719
706720
Parameters
707721
----------
708722
freq : a freq string indicating the ceiling resolution
723+
ambiguous : bool, 'NaT', default 'raise'
724+
- bool contains flags to determine if time is dst or not (note
725+
that this flag is only applicable for ambiguous fall dst dates)
726+
- 'NaT' will return NaT for an ambiguous time
727+
- 'raise' will raise an AmbiguousTimeError for an ambiguous time
728+
729+
Raises
730+
------
731+
ValueError if the freq cannot be converted
709732
"""
710-
return self._round(freq, np.ceil)
733+
return self._round(freq, np.ceil, ambiguous)
711734

712735
@property
713736
def tz(self):

pandas/tests/scalar/timestamp/test_unary_ops.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ def test_floor(self):
132132
expected = Timestamp('20130101')
133133
assert result == expected
134134

135+
@pytest.mark.parametrize('method', ['ceil', 'round', 'floor'])
136+
def test_round_dst_border(self, method):
137+
# GH 18946 round near DST
138+
ts = Timestamp('2017-10-29 00:00:00', tz='UTC').tz_convert(
139+
'Europe/Madrid'
140+
)
141+
#
142+
result = getattr(ts, method)('H', ambiguous=True)
143+
assert result == ts
144+
145+
result = getattr(ts, method)('H', ambiguous=False)
146+
expected = Timestamp('2017-10-29 01:00:00', tz='UTC').tz_convert(
147+
'Europe/Madrid'
148+
)
149+
assert result == expected
150+
151+
result = getattr(ts, method)('H', ambiguous='NaT')
152+
assert result is NaT
153+
154+
with pytest.raises(pytz.AmbiguousTimeError):
155+
getattr(ts, method)('H', ambiguous='raise')
156+
135157
# --------------------------------------------------------------
136158
# Timestamp.replace
137159

0 commit comments

Comments
 (0)