Skip to content

Commit 42dc1f7

Browse files
Stefan ScherfkeTomAugspurger
Stefan Scherfke
authored andcommitted
Fix issue pandas-dev#15683
Adjustments to coding guidelines Only perform timestamp() check if meth is available. Added sv
1 parent 9e55af2 commit 42dc1f7

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

asv_bench/benchmarks/timestamp.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytz
2+
import pandas as pd
3+
import datetime
4+
5+
6+
class TimeTimestamp(object):
7+
8+
goal_time = 0.5
9+
10+
def setup(self):
11+
self.ts = pd.Timestamp("2016-03-28") # after DST
12+
self.tzinfo = pytz.timezone("CET")
13+
14+
def time_replace(self):
15+
self.ts.replace(tzinfo=self.tzinfo).replace(tzinfo=None)
16+
17+

doc/source/whatsnew/v0.20.0.txt

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Highlights include:
2121
- Window binary corr/cov operations now return a MultiIndexed ``DataFrame`` rather than a ``Panel``, as ``Panel`` is now deprecated, see :ref:`here <whatsnew_0200.api_breaking.rolling_pairwise>`
2222
- Support for S3 handling now uses ``s3fs``, see :ref:`here <whatsnew_0200.api_breaking.s3>`
2323
- Google BigQuery support now uses the ``pandas-gbq`` library, see :ref:`here <whatsnew_0200.api_breaking.gbq>`
24+
- Switched the test framework to use `pytest <http://doc.pytest.org/en/latest>`__ (:issue:`13097`)
2425

2526
.. warning::
2627

@@ -1568,7 +1569,12 @@ Conversion
15681569

15691570
- Bug in ``Timestamp.replace`` now raises ``TypeError`` when incorrect argument names are given; previously this raised ``ValueError`` (:issue:`15240`)
15701571
- Bug in ``Timestamp.replace`` with compat for passing long integers (:issue:`15030`)
1572+
<<<<<<< HEAD
15711573
- Bug in ``Timestamp`` returning UTC based time/date attributes when a timezone was provided (:issue:`13303`, :issue:`6538`)
1574+
=======
1575+
- Bug in ``Timestamp.replace`` when replacing ``tzinfo`` around DST changes (:issue:`15683`)
1576+
- Bug in ``Timestamp`` returning UTC based time/date attributes when a timezone was provided (:issue:`13303`)
1577+
>>>>>>> Adjustments to coding guidelines
15721578
- Bug in ``Timestamp`` incorrectly localizing timezones during construction (:issue:`11481`, :issue:`15777`)
15731579
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
15741580
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)

pandas/_libs/tslib.pyx

+9-9
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,16 @@ class Timestamp(_Timestamp):
685685
cdef:
686686
pandas_datetimestruct dts
687687
int64_t value
688-
object _tzinfo, result, k, v
688+
object _tzinfo, result, k, v, ts_input
689689
_TSObject ts
690690

691691
# set to naive if needed
692692
_tzinfo = self.tzinfo
693693
value = self.value
694694
if _tzinfo is not None:
695-
value = tz_convert_single(value, 'UTC', _tzinfo)
695+
value_tz = tz_convert_single(value, _tzinfo, 'UTC')
696+
offset = value - value_tz
697+
value += offset
696698

697699
# setup components
698700
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
@@ -726,16 +728,14 @@ class Timestamp(_Timestamp):
726728
_tzinfo = tzinfo
727729

728730
# reconstruct & check bounds
729-
value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
731+
ts_input = datetime(dts.year, dts.month, dts.day, dts.hour, dts.min,
732+
dts.sec, dts.us, tzinfo=_tzinfo)
733+
ts = convert_to_tsobject(ts_input, _tzinfo, None, 0, 0)
734+
value = ts.value + (dts.ps // 1000)
730735
if value != NPY_NAT:
731736
_check_dts_bounds(&dts)
732737

733-
# set tz if needed
734-
if _tzinfo is not None:
735-
value = tz_convert_single(value, _tzinfo, 'UTC')
736-
737-
result = create_timestamp_from_ts(value, dts, _tzinfo, self.freq)
738-
return result
738+
return create_timestamp_from_ts(value, dts, _tzinfo, self.freq)
739739

740740
def isoformat(self, sep='T'):
741741
base = super(_Timestamp, self).isoformat(sep=sep)

pandas/tests/tseries/test_timezones.py

+21
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,27 @@ def test_ambiguous_compat(self):
12561256
assert (result_pytz.to_pydatetime().tzname() ==
12571257
result_dateutil.to_pydatetime().tzname())
12581258

1259+
def test_replace_tzinfo(self):
1260+
# GH 15683
1261+
dt = datetime(2016, 3, 27, 1)
1262+
tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo
1263+
1264+
result_dt = dt.replace(tzinfo=tzinfo)
1265+
result_pd = Timestamp(dt).replace(tzinfo=tzinfo)
1266+
1267+
if hasattr(result_dt, 'timestamp'): # New method in Py 3.3
1268+
assert result_dt.timestamp() == result_pd.timestamp()
1269+
assert result_dt == result_pd
1270+
assert result_dt == result_pd.to_pydatetime()
1271+
1272+
result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None)
1273+
result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None)
1274+
1275+
if hasattr(result_dt, 'timestamp'): # New method in Py 3.3
1276+
assert result_dt.timestamp() == result_pd.timestamp()
1277+
assert result_dt == result_pd
1278+
assert result_dt == result_pd.to_pydatetime()
1279+
12591280
def test_index_equals_with_tz(self):
12601281
left = date_range('1/1/2011', periods=100, freq='H', tz='utc')
12611282
right = date_range('1/1/2011', periods=100, freq='H', tz='US/Eastern')

0 commit comments

Comments
 (0)