Skip to content

Commit 494fb99

Browse files
committed
move array_to_datetime timests
1 parent d7797b4 commit 494fb99

File tree

2 files changed

+154
-166
lines changed

2 files changed

+154
-166
lines changed

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 9 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from pandas.errors import OutOfBoundsDatetime
2121
from pandas.compat import lmap, PY3
22-
from pandas.compat.numpy import np_array_datetime64_compat
2322
from pandas.core.dtypes.common import is_datetime64_ns_dtype
2423
from pandas.util import testing as tm
2524
import pandas.util._test_decorators as td
@@ -791,6 +790,15 @@ def test_dataframe_dtypes(self, cache):
791790

792791

793792
class TestToDatetimeMisc(object):
793+
def test_to_datetime_barely_out_of_bounds(self):
794+
# GH#19529
795+
# GH#19382 close enough to bounds that dropping nanos would result
796+
# in an in-bounds datetime
797+
arr = np.array(['2262-04-11 23:47:16.854775808'], dtype=object)
798+
799+
with pytest.raises(OutOfBoundsDatetime):
800+
to_datetime(arr)
801+
794802
@pytest.mark.parametrize('cache', [True, False])
795803
def test_to_datetime_iso8601(self, cache):
796804
result = to_datetime(["2012-01-01 00:00:00"], cache=cache)
@@ -1452,171 +1460,6 @@ def test_parsers_timezone_minute_offsets_roundtrip(self, cache):
14521460
converted_time = dt_time.tz_localize('UTC').tz_convert(tz)
14531461
assert dt_string_repr == repr(converted_time)
14541462

1455-
def test_parsers_iso8601(self):
1456-
# GH 12060
1457-
# test only the iso parser - flexibility to different
1458-
# separators and leadings 0s
1459-
# Timestamp construction falls back to dateutil
1460-
cases = {'2011-01-02': datetime(2011, 1, 2),
1461-
'2011-1-2': datetime(2011, 1, 2),
1462-
'2011-01': datetime(2011, 1, 1),
1463-
'2011-1': datetime(2011, 1, 1),
1464-
'2011 01 02': datetime(2011, 1, 2),
1465-
'2011.01.02': datetime(2011, 1, 2),
1466-
'2011/01/02': datetime(2011, 1, 2),
1467-
'2011\\01\\02': datetime(2011, 1, 2),
1468-
'2013-01-01 05:30:00': datetime(2013, 1, 1, 5, 30),
1469-
'2013-1-1 5:30:00': datetime(2013, 1, 1, 5, 30)}
1470-
for date_str, exp in compat.iteritems(cases):
1471-
actual = tslib._test_parse_iso8601(date_str)
1472-
assert actual == exp
1473-
1474-
# separators must all match - YYYYMM not valid
1475-
invalid_cases = ['2011-01/02', '2011^11^11',
1476-
'201401', '201111', '200101',
1477-
# mixed separated and unseparated
1478-
'2005-0101', '200501-01',
1479-
'20010101 12:3456', '20010101 1234:56',
1480-
# HHMMSS must have two digits in each component
1481-
# if unseparated
1482-
'20010101 1', '20010101 123', '20010101 12345',
1483-
'20010101 12345Z',
1484-
# wrong separator for HHMMSS
1485-
'2001-01-01 12-34-56']
1486-
for date_str in invalid_cases:
1487-
with pytest.raises(ValueError):
1488-
tslib._test_parse_iso8601(date_str)
1489-
# If no ValueError raised, let me know which case failed.
1490-
raise Exception(date_str)
1491-
1492-
1493-
class TestArrayToDatetime(object):
1494-
def test_parsing_valid_dates(self):
1495-
arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
1496-
tm.assert_numpy_array_equal(
1497-
tslib.array_to_datetime(arr),
1498-
np_array_datetime64_compat(
1499-
[
1500-
'2013-01-01T00:00:00.000000000-0000',
1501-
'2013-01-02T00:00:00.000000000-0000'
1502-
],
1503-
dtype='M8[ns]'
1504-
)
1505-
)
1506-
1507-
arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object)
1508-
tm.assert_numpy_array_equal(
1509-
tslib.array_to_datetime(arr),
1510-
np_array_datetime64_compat(
1511-
[
1512-
'2013-09-16T00:00:00.000000000-0000',
1513-
'2013-09-17T00:00:00.000000000-0000'
1514-
],
1515-
dtype='M8[ns]'
1516-
)
1517-
)
1518-
1519-
def test_parsing_timezone_offsets(self):
1520-
# All of these datetime strings with offsets are equivalent
1521-
# to the same datetime after the timezone offset is added
1522-
dt_strings = [
1523-
'01-01-2013 08:00:00+08:00',
1524-
'2013-01-01T08:00:00.000000000+0800',
1525-
'2012-12-31T16:00:00.000000000-0800',
1526-
'12-31-2012 23:00:00-01:00'
1527-
]
1528-
1529-
expected_output = tslib.array_to_datetime(np.array(
1530-
['01-01-2013 00:00:00'], dtype=object))
1531-
1532-
for dt_string in dt_strings:
1533-
tm.assert_numpy_array_equal(
1534-
tslib.array_to_datetime(
1535-
np.array([dt_string], dtype=object)
1536-
),
1537-
expected_output
1538-
)
1539-
1540-
def test_number_looking_strings_not_into_datetime(self):
1541-
# #4601
1542-
# These strings don't look like datetimes so they shouldn't be
1543-
# attempted to be converted
1544-
arr = np.array(['-352.737091', '183.575577'], dtype=object)
1545-
tm.assert_numpy_array_equal(
1546-
tslib.array_to_datetime(arr, errors='ignore'), arr)
1547-
1548-
arr = np.array(['1', '2', '3', '4', '5'], dtype=object)
1549-
tm.assert_numpy_array_equal(
1550-
tslib.array_to_datetime(arr, errors='ignore'), arr)
1551-
1552-
def test_coercing_dates_outside_of_datetime64_ns_bounds(self):
1553-
invalid_dates = [
1554-
date(1000, 1, 1),
1555-
datetime(1000, 1, 1),
1556-
'1000-01-01',
1557-
'Jan 1, 1000',
1558-
np.datetime64('1000-01-01'),
1559-
]
1560-
1561-
for invalid_date in invalid_dates:
1562-
pytest.raises(ValueError,
1563-
tslib.array_to_datetime,
1564-
np.array([invalid_date], dtype='object'),
1565-
errors='raise', )
1566-
tm.assert_numpy_array_equal(
1567-
tslib.array_to_datetime(
1568-
np.array([invalid_date], dtype='object'),
1569-
errors='coerce'),
1570-
np.array([tslib.iNaT], dtype='M8[ns]')
1571-
)
1572-
1573-
arr = np.array(['1/1/1000', '1/1/2000'], dtype=object)
1574-
tm.assert_numpy_array_equal(
1575-
tslib.array_to_datetime(arr, errors='coerce'),
1576-
np_array_datetime64_compat(
1577-
[
1578-
tslib.iNaT,
1579-
'2000-01-01T00:00:00.000000000-0000'
1580-
],
1581-
dtype='M8[ns]'
1582-
)
1583-
)
1584-
1585-
def test_coerce_of_invalid_datetimes(self):
1586-
arr = np.array(['01-01-2013', 'not_a_date', '1'], dtype=object)
1587-
1588-
# Without coercing, the presence of any invalid dates prevents
1589-
# any values from being converted
1590-
tm.assert_numpy_array_equal(
1591-
tslib.array_to_datetime(arr, errors='ignore'), arr)
1592-
1593-
# With coercing, the invalid dates becomes iNaT
1594-
tm.assert_numpy_array_equal(
1595-
tslib.array_to_datetime(arr, errors='coerce'),
1596-
np_array_datetime64_compat(
1597-
[
1598-
'2013-01-01T00:00:00.000000000-0000',
1599-
tslib.iNaT,
1600-
tslib.iNaT
1601-
],
1602-
dtype='M8[ns]'
1603-
)
1604-
)
1605-
1606-
def test_to_datetime_barely_out_of_bounds(self):
1607-
# GH#19529
1608-
# GH#19382 close enough to bounds that dropping nanos would result
1609-
# in an in-bounds datetime
1610-
arr = np.array(['2262-04-11 23:47:16.854775808'], dtype=object)
1611-
1612-
with pytest.raises(OutOfBoundsDatetime):
1613-
to_datetime(arr)
1614-
1615-
with pytest.raises(OutOfBoundsDatetime):
1616-
# Essentially the same as above, but more directly calling
1617-
# the relevant function
1618-
tslib.array_to_datetime(arr)
1619-
16201463

16211464
def test_normalize_date():
16221465
value = date(2012, 9, 7)
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# -*- coding: utf-8 -*-
2+
from datetime import datetime, date
3+
4+
import numpy as np
5+
import pytest
6+
7+
from pandas import compat
8+
from pandas._libs import tslib
9+
from pandas.compat.numpy import np_array_datetime64_compat
10+
import pandas.util.testing as tm
11+
12+
13+
class TestParseISO8601(object):
14+
def test_parsers_iso8601(self):
15+
# GH#12060
16+
# test only the iso parser - flexibility to different
17+
# separators and leadings 0s
18+
# Timestamp construction falls back to dateutil
19+
cases = {'2011-01-02': datetime(2011, 1, 2),
20+
'2011-1-2': datetime(2011, 1, 2),
21+
'2011-01': datetime(2011, 1, 1),
22+
'2011-1': datetime(2011, 1, 1),
23+
'2011 01 02': datetime(2011, 1, 2),
24+
'2011.01.02': datetime(2011, 1, 2),
25+
'2011/01/02': datetime(2011, 1, 2),
26+
'2011\\01\\02': datetime(2011, 1, 2),
27+
'2013-01-01 05:30:00': datetime(2013, 1, 1, 5, 30),
28+
'2013-1-1 5:30:00': datetime(2013, 1, 1, 5, 30)}
29+
for date_str, exp in compat.iteritems(cases):
30+
actual = tslib._test_parse_iso8601(date_str)
31+
assert actual == exp
32+
33+
@pytest.mark.parametrize('date_str', ['2011-01/02', '2011^11^11',
34+
'201401', '201111', '200101',
35+
# mixed separated and unseparated
36+
'2005-0101', '200501-01',
37+
'20010101 12:3456',
38+
'20010101 1234:56',
39+
# HHMMSS must have two digits in
40+
# each component if unseparated
41+
'20010101 1', '20010101 123',
42+
'20010101 12345', '20010101 12345Z',
43+
# wrong separator for HHMMSS
44+
'2001-01-01 12-34-56'])
45+
def test_parsers_iso8601_invalid(self, date_str):
46+
# separators must all match - YYYYMM not valid
47+
with pytest.raises(ValueError):
48+
tslib._test_parse_iso8601(date_str)
49+
# If no ValueError raised, let me know which case failed.
50+
raise Exception(date_str)
51+
52+
53+
class TestArrayToDatetime(object):
54+
def test_parsing_valid_dates(self):
55+
arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
56+
result = tslib.array_to_datetime(arr)
57+
expected = ['2013-01-01T00:00:00.000000000-0000',
58+
'2013-01-02T00:00:00.000000000-0000']
59+
tm.assert_numpy_array_equal(result,
60+
np_array_datetime64_compat(expected,
61+
dtype='M8[ns]'))
62+
63+
arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object)
64+
result = tslib.array_to_datetime(arr)
65+
expected = ['2013-09-16T00:00:00.000000000-0000',
66+
'2013-09-17T00:00:00.000000000-0000']
67+
tm.assert_numpy_array_equal(result,
68+
np_array_datetime64_compat(expected,
69+
dtype='M8[ns]'))
70+
71+
@pytest.mark.parametrize('dt_string', [
72+
'01-01-2013 08:00:00+08:00',
73+
'2013-01-01T08:00:00.000000000+0800',
74+
'2012-12-31T16:00:00.000000000-0800',
75+
'12-31-2012 23:00:00-01:00'])
76+
def test_parsing_timezone_offsets(self, dt_string):
77+
# All of these datetime strings with offsets are equivalent
78+
# to the same datetime after the timezone offset is added
79+
arr = np.array(['01-01-2013 00:00:00'], dtype=object)
80+
expected = tslib.array_to_datetime(arr)
81+
82+
arr = np.array([dt_string], dtype=object)
83+
result = tslib.array_to_datetime(arr)
84+
tm.assert_numpy_array_equal(result, expected)
85+
86+
def test_number_looking_strings_not_into_datetime(self):
87+
# GH#4601
88+
# These strings don't look like datetimes so they shouldn't be
89+
# attempted to be converted
90+
arr = np.array(['-352.737091', '183.575577'], dtype=object)
91+
result = tslib.array_to_datetime(arr, errors='ignore')
92+
tm.assert_numpy_array_equal(result, arr)
93+
94+
arr = np.array(['1', '2', '3', '4', '5'], dtype=object)
95+
result = tslib.array_to_datetime(arr, errors='ignore')
96+
tm.assert_numpy_array_equal(result, arr)
97+
98+
@pytest.mark.parametrize('invalid_date', [date(1000, 1, 1),
99+
datetime(1000, 1, 1),
100+
'1000-01-01',
101+
'Jan 1, 1000',
102+
np.datetime64('1000-01-01')])
103+
def test_coerce_outside_ns_bounds(self, invalid_date):
104+
arr = np.array([invalid_date], dtype='object')
105+
with pytest.raises(ValueError):
106+
tslib.array_to_datetime(arr, errors='raise')
107+
108+
result = tslib.array_to_datetime(arr, errors='coerce')
109+
expected = np.array([tslib.iNaT], dtype='M8[ns]')
110+
tm.assert_numpy_array_equal(result, expected)
111+
112+
def test_coerce_outside_ns_bounds_one_valid(self):
113+
arr = np.array(['1/1/1000', '1/1/2000'], dtype=object)
114+
result = tslib.array_to_datetime(arr, errors='coerce')
115+
expected = [tslib.iNaT,
116+
'2000-01-01T00:00:00.000000000-0000'],
117+
tm.assert_numpy_array_equal(result,
118+
np_array_datetime64_compat(expected,
119+
dtype='M8[ns]'))
120+
121+
def test_coerce_of_invalid_datetimes(self):
122+
arr = np.array(['01-01-2013', 'not_a_date', '1'], dtype=object)
123+
124+
# Without coercing, the presence of any invalid dates prevents
125+
# any values from being converted
126+
result = tslib.array_to_datetime(arr, errors='ignore')
127+
tm.assert_numpy_array_equal(result, arr)
128+
129+
# With coercing, the invalid dates becomes iNaT
130+
result = tslib.array_to_datetime(arr, errors='coerce')
131+
expected = ['2013-01-01T00:00:00.000000000-0000',
132+
tslib.iNaT,
133+
tslib.iNaT]
134+
135+
tm.assert_numpy_array_equal(result,
136+
np_array_datetime64_compat(expected,
137+
dtype='M8[ns]'))
138+
139+
def test_to_datetime_barely_out_of_bounds(self):
140+
# GH#19529
141+
# GH#19382 close enough to bounds that dropping nanos would result
142+
# in an in-bounds datetime
143+
arr = np.array(['2262-04-11 23:47:16.854775808'], dtype=object)
144+
with pytest.raises(tslib.OutOfBoundsDatetime):
145+
tslib.array_to_datetime(arr)

0 commit comments

Comments
 (0)