8000 BUG/API: to_datetime preserves UTC offsets when parsing datetime strings by mroeschke · Pull Request #21822 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG/API: to_datetime preserves UTC offsets when parsing datetime strings #21822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ac5a3d1
BUG: to_datetime no longer converts offsets to UTC
Jul 7, 2018
b81a8e9
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 8, 2018
6bf46a8
Document and now return offset
Jul 8, 2018
678b337
Add some tests, start converting some existing uses of array_to_datetime
Jul 8, 2018
1917148
Add more tests
Jul 8, 2018
581a33e
Adjust test
Jul 8, 2018
a1bc8f9
Flake8
Jul 8, 2018
80042e6
Add tests confirming new behavior
Jul 8, 2018
7c4135e
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 10, 2018
0651416
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 11, 2018
bacb6e3
Lint
Jul 11, 2018
a2f4aad
adjust a test
Jul 11, 2018
d48f341
Ensure box object index, pass tests
Jul 11, 2018
1d527ff
Adjust test
Jul 11, 2018
f89d6b6
Cleanup and add comments
Jul 12, 2018
d91c63f
address comments
Jul 12, 2018
1054e8b
adjust test to be closer to the original behavior
Jul 12, 2018
d9cdc91
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 12, 2018
7d04613
Add TypeError clause
Jul 12, 2018
031284c
Add TypeError not ValueError
Jul 12, 2018
749e62e
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 12, 2018
23cbf75
fix typo
Jul 12, 2018
1e6f87a
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 18, 2018
7539bcf
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 19, 2018
c1f51cd
New implimentation
Jul 19, 2018
db75a24
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 20, 2018
4733ac5
Change implimentation and add some tests
Jul 20, 2018
e3db735
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 20, 2018
2fa681f
Add missing commas
Jul 20, 2018
5f36c98
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 24, 2018
d7ff275
Change implimentation since tzoffsets cannot be hashed
Jul 25, 2018
4ff7cb3
Add whatsnew
Jul 25, 2018
8463d91
Address review
Jul 25, 2018
dddc6b3
Address tzlocal
Jul 25, 2018
cca3983
Change is to == for older dateutil compat
Jul 26, 2018
e441be0
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 26, 2018
a8a65f7
Modify example in whatsnew to display
Jul 26, 2018
75f9fd9
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 26, 2018
6052475
Add more specific errors
Jul 27, 2018
f916c69
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 28, 2018
807a251
Merge remote-tracking branch 'upstream/master' into parse_tz_offsets
Jul 29, 2018
1cbd9b9
Add some benchmarks and reformat tests
Jul 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add some tests, start converting some existing uses of array_to_datetime
  • Loading branch information
Matt Roeschke committed Jul 8, 2018
commit 678b337b80b2f9d8f4b2662376d0f6f9b846274e
2 changes: 1 addition & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',

if seen_datetime_offset:
# GH 17697
# 1) If all the offsets are equal, then return 1, pytz.FixedOffset for the
# 1) If all the offsets are equal, then return one pytz.FixedOffset for the
# parsed dates so it can behave nicely with DatetimeIndex
# 2) If the offsets are different, then force the parsing down the object path
# where an array of datetimes (with individual datutil.tzoffsets) are returned
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def try_datetime(v):
# GH19671
v = tslib.array_to_datetime(v,
require_iso8601=True,
errors='raise')
errors='raise')[0]
except ValueError:

# we might have a sequence of the same-datetimes with tz's
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
result = arg

if result is None and (format is None or infer_datetime_format):
result = tslib.array_to_datetime(
result, tz_parsed = tslib.array_to_datetime(
arg,
errors=errors,
utc=tz == 'utc',
Expand Down Expand Up @@ -696,7 +696,7 @@ def calc(carg):
parsed = parsing.try_parse_year_month_day(carg / 10000,
carg / 100 % 100,
carg % 100)
return tslib.array_to_datetime(parsed, errors=errors)
return tslib.array_to_datetime(parsed, errors=errors)[0]

def calc_with_mask(carg, mask):
result = np.empty(carg.shape, dtype='M8[ns]')
Expand Down
37 changes: 21 additions & 16 deletions pandas/tests/tslibs/test_array_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import pytest
import pytz

from pandas._libs import tslib
from pandas.compat.numpy import np_array_datetime64_compat
Expand Down Expand Up @@ -52,46 +53,50 @@ def test_parsers_iso8601_invalid(self, date_str):
class TestArrayToDatetime(object):
def test_parsing_valid_dates(self):
arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
result = tslib.array_to_datetime(arr)
result = tslib.array_to_datetime(arr)[0]
expected = ['2013-01-01T00:00:00.000000000-0000',
'2013-01-02T00:00:00.000000000-0000']
tm.assert_numpy_array_equal(
result,
np_array_datetime64_compat(expected, dtype='M8[ns]'))

arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object)
result = tslib.array_to_datetime(arr)
result = tslib.array_to_datetime(arr)[0]
expected = ['2013-09-16T00:00:00.000000000-0000',
'2013-09-17T00:00:00.000000000-0000']
tm.assert_numpy_array_equal(
result,
np_array_datetime64_compat(expected, dtype='M8[ns]'))

@pytest.mark.parametrize('dt_string', [
'01-01-2013 08:00:00+08:00',
'2013-01-01T08:00:00.000000000+0800',
'2012-12-31T16:00:00.000000000-0800',
'12-31-2012 23:00:00-01:00'])
def test_parsing_timezone_offsets(self, dt_string):
@pytest.mark.parametrize('dt_string, expected_tz', [
['01-01-2013 08:00:00+08:00', None],
['2013-01-01T08:00:00.000000000+0800', pytz.FixedOffset(480)],
['2012-12-31T16:00:00.000000000-0800', pytz.FixedOffset(-480)],
['12-31-2012 23:00:00-01:00', None]])
def test_parsing_timezone_offsets(self, dt_string, expected_tz):
# All of these datetime strings with offsets are equivalent
# to the same datetime after the timezone offset is added

# TODO: Appears that the dateparser doesnt return offset info if string is non-ISO
# maybe something in the np_datetime_strings parser is not catching this?
arr = np.array(['01-01-2013 00:00:00'], dtype=object)
expected = tslib.array_to_datetime(arr)
expected = tslib.array_to_datetime(arr)[0]

arr = np.array([dt_string], dtype=object)
result = tslib.array_to_datetime(arr)
result, result_tz = tslib.array_to_datetime(arr)
tm.assert_numpy_array_equal(result, expected)
assert result_tz is expected_tz

def test_number_looking_strings_not_into_datetime(self):
# GH#4601
# These strings don't look like datetimes so they shouldn't be
# attempted to be converted
arr = np.array(['-352.737091', '183.575577'], dtype=object)
result = tslib.array_to_datetime(arr, errors='ignore')
result = tslib.array_to_datetime(arr, errors='ignore')[0]
tm.assert_numpy_array_equal(result, arr)

arr = np.array(['1', '2', '3', '4', '5'], dtype=object)
result = tslib.array_to_datetime(arr, errors='ignore')
result = tslib.array_to_datetime(arr, errors='ignore')[0]
tm.assert_numpy_array_equal(result, arr)

@pytest.mark.parametrize('invalid_date', [
Expand All @@ -105,13 +110,13 @@ def test_coerce_outside_ns_bounds(self, invalid_date):
with pytest.raises(ValueError):
tslib.array_to_datetime(arr, errors='raise')

result = tslib.array_to_datetime(arr, errors='coerce')
result = tslib.array_to_datetime(arr, errors='coerce')[0]
expected = np.array([tslib.iNaT], dtype='M8[ns]')
tm.assert_numpy_array_equal(result, expected)

def test_coerce_outside_ns_bounds_one_valid(self):
arr = np.array(['1/1/1000', '1/1/2000'], dtype=object)
result = tslib.array_to_datetime(arr, errors='coerce')
result = tslib.array_to_datetime(arr, errors='coerce')[0]
expected = [tslib.iNaT,
'2000-01-01T00:00:00.000000000-0000']
tm.assert_numpy_array_equal(
Expand All @@ -123,11 +128,11 @@ def test_coerce_of_invalid_datetimes(self):

# Without coercing, the presence of any invalid dates prevents
# any values from being converted
result = tslib.array_to_datetime(arr, errors='ignore')
result = tslib.array_to_datetime(arr, errors='ignore')[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like writing these like

result, _ = ...... I think its more clear

tm.assert_numpy_array_equal(result, arr)

# With coercing, the invalid dates becomes iNaT
result = tslib.array_to_datetime(arr, errors='coerce')
result = tslib.array_to_datetime(arr, errors='coerce')[0]
expected = ['2013-01-01T00:00:00.000000000-0000',
tslib.iNaT,
tslib.iNaT]
Expand Down
0