Skip to content

Commit eb7e49d

Browse files
committed
Work around a Pandas 0.23 bug
Work around an old time zone related bug (pandas-dev/pandas#11736), which was fixed in Pandas 0.24.
1 parent 3c1a63d commit eb7e49d

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

HISTORY.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
History
33
=======
44

5+
4.0.2 (2022-11-27)
6+
==================
7+
8+
- Worked around an old Pandas bug related to time zones
9+
(https://github.com/pandas-dev/pandas/issues/11736) in order to
10+
maintain compatibility with Pandas 0.23 (the bug was fixed in Pandas
11+
0.24).
12+
513
4.0.1 (2022-11-27)
614
==================
715

htimeseries/htimeseries.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,11 @@ def _read_two_columns_from_stream(self, f):
468468
dtype={"value": np.float64},
469469
)
470470
result["flags"] = ""
471+
self._ensure_pandas_0_23_compatibility(result)
471472
return result
472473

473474
def _read_three_columns_from_stream(self, f):
474-
return pd.read_csv(
475+
result = pd.read_csv(
475476
f,
476477
parse_dates=[0],
477478
names=("date", "value", "flags"),
@@ -484,6 +485,16 @@ def _read_three_columns_from_stream(self, f):
484485
},
485486
dtype={"value": np.float64},
486487
)
488+
self._ensure_pandas_0_23_compatibility(result)
489+
return result
490+
491+
def _ensure_pandas_0_23_compatibility(self, result):
492+
# Workaround for https://github.com/pandas-dev/pandas/issues/11736, which was
493+
# fixed in 0.24.
494+
if len(result) and result.index.tz is None:
495+
result.index = result.index.tz_localize(dt.timezone.utc).tz_convert(
496+
self.tzinfo
497+
)
487498

488499
def _check_there_are_no_duplicates(self, data):
489500
_check_timeseries_index_has_no_duplicates(

tests/test_htimeseries.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ def test_flags(self):
432432
expected = np.array(["", "MISS", "", "", ""])
433433
np.testing.assert_array_equal(self.ts.data.values[:, 1], expected)
434434

435+
def test_tz(self):
436+
self.assertEqual(
437+
self.ts.data.index.tz.utcoffset(dt.datetime(2000, 1, 1)),
438+
dt.timedelta(hours=2),
439+
)
440+
435441

436442
class HTimeseriesReadTwoColumnsTestCase(ReadFilelikeTestCaseBase, TestCase):
437443
def setUp(self):
@@ -447,6 +453,12 @@ def test_flags(self):
447453
expected = np.array(["", "", "", "", ""])
448454
np.testing.assert_array_equal(self.ts.data.values[:, 1], expected)
449455

456+
def test_tz(self):
457+
self.assertEqual(
458+
self.ts.data.index.tz.utcoffset(dt.datetime(2000, 1, 1)),
459+
dt.timedelta(hours=2),
460+
)
461+
450462

451463
class HTimeseriesReadMixOf2And3ColumnsTestCase(ReadFilelikeTestCaseBase, TestCase):
452464
def setUp(self):

0 commit comments

Comments
 (0)