From cbc8c73fdf617a4644baa130dff05476c7a61645 Mon Sep 17 00:00:00 2001 From: ischwabacher Date: Mon, 17 Feb 2014 18:06:36 -0600 Subject: [PATCH 1/3] Fix behavior of `to_offset` with leading zeroes Currently, `pandas.tseries.frequencies.to_offset` erroneously returns a zero time offset when the first part of its argument has a numerical value of zero, even if later parts have nonzero values. For instance, In [123]: pandas.tseries.frequencies.to_offset('00H 00T 01S') Out[123]: <0 * Days> In this patch the sign check is applied before conversion to `int` in order to support offsets like `'-00H 00T 01S'`. --- pandas/tseries/frequencies.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 3892897e43bb0..398e428e45c79 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -283,11 +283,11 @@ def to_offset(freqstr): try: for stride, name, _ in opattern.findall(freqstr): offset = get_offset(name) + if stride_sign is None: + stride_sign = -1 if stride.startswith('-') else 1 if not stride: stride = 1 stride = int(stride) - if stride_sign is None: - stride_sign = np.sign(stride) offset = offset * int(np.fabs(stride) * stride_sign) if delta is None: delta = offset From d5c948432e50aa35f2ac8d6f4c395c99bf79b62e Mon Sep 17 00:00:00 2001 From: ischwabacher Date: Mon, 17 Feb 2014 18:50:50 -0600 Subject: [PATCH 2/3] TST: Add tests for `to_offset` with leading zeroes Test for the issues fixed by cbc8c73fdf617a4644baa130dff05476c7a61645. --- pandas/tseries/tests/test_frequencies.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tseries/tests/test_frequencies.py b/pandas/tseries/tests/test_frequencies.py index 876204d2275e7..3e8600af36f79 100644 --- a/pandas/tseries/tests/test_frequencies.py +++ b/pandas/tseries/tests/test_frequencies.py @@ -72,6 +72,16 @@ def test_to_offset_negative(): freqstr = '-5min10s' result = to_offset(freqstr) assert(result.n == -310) + + +def test_to_offset_leading_zero(): + freqstr = '00H 00T 01S' + result = to_offset(freqstr) + assert(result.n == 1) + + freqstr = '-00H 03T 14S' + result = to_offset(freqstr) + assert(result.n == -194) def test_anchored_shortcuts(): From b9fb1289c300aed318ac16ca46abbdb5fb6e4543 Mon Sep 17 00:00:00 2001 From: ischwabacher Date: Tue, 18 Feb 2014 09:51:23 -0600 Subject: [PATCH 3/3] Add `to_offset` fix to release notes --- doc/source/release.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/release.rst b/doc/source/release.rst index 187f1a97c8f0e..d3323762ec6c3 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -96,6 +96,7 @@ Improvements to existing features Bug Fixes ~~~~~~~~~ +- Bug in ``pd.tseries.frequencies.to_offset`` when argument has leading zeroes (:issue:`6391`) - Bug in version string gen. for dev versions with shallow clones / install from tarball (:issue:`6127`) - Inconsistent tz parsing Timestamp/to_datetime for current year (:issue:`5958`) - Indexing bugs with reordered indexes (:issue:`6252`, :issue:`6254`)