Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Mar 10, 2018
1 parent 848b1b0 commit a6c61d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
14 changes: 8 additions & 6 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from pandas.core import algorithms
from pandas.compat import PY3, zip


def _guess_datetime_format_for_array(arr, **kwargs):
# Try to guess the format based on the first non-NaN element
non_nan_elements = notna(arr).nonzero()[0]
Expand Down Expand Up @@ -362,11 +363,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
tz=tz,
name=name)
else:
stamps = [tslibs.Timestamp(res, tz=tz)
stamps = [tslib.Timestamp(res, tz=tz)
for res in result]
result = np.array(stamps, dtype=object)
else:
stamps = [tslibs.Timestamp(res, tz=tz)
stamps = [tslib.Timestamp(res, tz=tz)
for res, tz in zip(result, tznames)]
result = np.array(stamps, dtype=object)
return result
Expand All @@ -381,16 +382,17 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
tz=tzoffset,
name=name)
else:
stamps = [tslibs.Timestamp(res,
tzinfo=tzoffset)
for res, offset in result]
stamps = [tslib.Timestamp(res,
tzinfo=tzoffset)
for res, offset in
zip(result, tzoffsets)]
result = np.array(stamps, dtype=object)
else:
stamps = []
for res, offset in zip(result, tzoffsets):
offset_mins = offset.total_seconds() / 60
tzoffset = pytz.FixedOffset(offset_mins)
ts = tslibs.Timestamp(res, tzinfo=tzoffset)
ts = tslib.Timestamp(res, tzinfo=tzoffset)
stamps.append(ts)
result = np.array(stamps, dtype=object)
return result
Expand Down
33 changes: 30 additions & 3 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def test_to_datetime_format_weeks(self, cache):
for s, format, dt in data:
assert to_datetime(s, format=format, cache=cache) == dt

@pytest.skipif(not PY3, reason="datetime.timezone not supported in PY2")
@pytest.mark.skipif(not PY3,
reason="datetime.timezone not supported in PY2")
@pytest.mark.xfail(reason="GH #20257")
def test_to_datetime_parse_timezone(self):
from datetime import timezone
# %Z parsing only
Expand All @@ -195,6 +197,17 @@ def test_to_datetime_parse_timezone(self):
expected = pd.DatetimeIndex(expected_dates)
tm.assert_index_equal(result, expected)

result = pd.to_datetime(dates, format=fmt, box=False)
expected = np.array(expected_dates, dtype=object)
tm.assert_numpy_array_equal(result, expected)

dates = ['2010-01-01 12:00:00 UTC', '2010-01-01 12:00:00 GMT']
result = pd.to_datetime(dates, format=fmt)
expected_dates = [pd.Timestamp('2010-01-01 12:00:00', tz='UTC'),
pd.Timestamp('2010-01-01 12:00:00', tz='GMT')]
expected = np.array(expected_dates, dtype=object)
tm.assert_numpy_array_equal(result, expected)

# %z parsing only
dates = ['2010-01-01 12:00:00 +0100'] * 2
fmt = '%Y-%m-%d %H:%M:%S %z'
Expand All @@ -204,17 +217,31 @@ def test_to_datetime_parse_timezone(self):
expected = pd.DatetimeIndex(expected_dates)
tm.assert_index_equal(result, expected)

result = pd.to_datetime(dates, format=fmt, box=False)
expected = np.array(expected_dates, dtype=object)
tm.assert_numpy_array_equal(result, expected)

dates = ['2010-01-01 12:00:00 +0100', '2010-01-01 12:00:00 -0100']
result = pd.to_datetime(dates, format=fmt)
expected_dates = [pd.Timestamp('2010-01-01 12:00:00',
tzinfo=pytz.FixedOffset(60)),
pd.Timestamp('2010-01-01 12:00:00',
tzinfo=pytz.FixedOffset(-60))]
expected = np.array(expected_dates, dtype=object)
tm.assert_numpy_array_equal(result, expected)

# %z and %Z parsing
dates = ['2010-01-01 12:00:00 UTC +0100'] * 2
fmt = '%Y-%m-%d %H:%M:%S %Z %z'
result = pd.to_datetime(dates, format=fmt)
tzinfo = timezone(timedelta(minutes=60), 'UTC')
expected_dates = [pd.Timestamp('2010-01-01 12:00:00', tzinfo=tzinfo)]
expected_dates = [pd.Timestamp('2010-01-01 13:00:00', tzinfo=tzinfo)]
expected = np.array(expected_dates * 2, dtype=object)
tm.assert_numpy_array_equal(result, expected)

with pytest.raises(ValueError):
pd.to_datetime(dates, format=fmt, tz='US/Pacific')
pd.to_datetime(dates, format=fmt, utc=True)


class TestToDatetime(object):
def test_to_datetime_pydatetime(self):
Expand Down

0 comments on commit a6c61d8

Please sign in to comment.