Skip to content

Commit da92a5c

Browse files
TrigonaMinimajreback
TrigonaMinima
authored andcommitted
TST: DatetimeIndex compiled together in test_datetime.py
xref #14854 Author: TrigonaMinima <[email protected]> Closes #15266 from TrigonaMinima/issue-14854-datetime and squashes the following commits: 6ee2bd9 [TrigonaMinima] TST: Splitting test_datetime.py into smaller chunks (gh14854) 415a748 [TrigonaMinima] TST: Moving DatetimeIndex related tests from test_timeseries.py and flake8 fixes c43c7de [TrigonaMinima] TST: proper naming of files 458d141 [TrigonaMinima] TST: splitting test_datetime.py 1ff0819 [TrigonaMinima] TST: fix flake8 errors - test_datetime.py (GH14854) 9311161 [TrigonaMinima] TST: reorg of DatetimeIndex tests from tseries/tests/test_base.py to test_datetime.py (GH14854) 54421a5 [TrigonaMinima] TST: reorg of DatetimeIndex tests from test_datetimelike.py to test_datetime.py (GH14854) f83814b [TrigonaMinima] TST: reorg of DatetimeIndex tests from test_timeseries.py to test_datetime.py
1 parent f6cfaab commit da92a5c

13 files changed

+3259
-3081
lines changed

pandas/tests/indexes/datetimes/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import numpy as np
2+
3+
import pandas as pd
4+
import pandas.util.testing as tm
5+
from pandas import (DatetimeIndex, date_range, Series, NaT, Index, Timestamp,
6+
Int64Index)
7+
8+
9+
class TestDatetimeIndex(tm.TestCase):
10+
_multiprocess_can_split_ = True
11+
12+
def test_astype(self):
13+
# GH 13149, GH 13209
14+
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
15+
16+
result = idx.astype(object)
17+
expected = Index([Timestamp('2016-05-16')] + [NaT] * 3, dtype=object)
18+
tm.assert_index_equal(result, expected)
19+
20+
result = idx.astype(int)
21+
expected = Int64Index([1463356800000000000] +
22+
[-9223372036854775808] * 3, dtype=np.int64)
23+
tm.assert_index_equal(result, expected)
24+
25+
rng = date_range('1/1/2000', periods=10)
26+
result = rng.astype('i8')
27+
self.assert_index_equal(result, Index(rng.asi8))
28+
self.assert_numpy_array_equal(result.values, rng.asi8)
29+
30+
def test_astype_with_tz(self):
31+
32+
# with tz
33+
rng = date_range('1/1/2000', periods=10, tz='US/Eastern')
34+
result = rng.astype('datetime64[ns]')
35+
expected = (date_range('1/1/2000', periods=10,
36+
tz='US/Eastern')
37+
.tz_convert('UTC').tz_localize(None))
38+
tm.assert_index_equal(result, expected)
39+
40+
# BUG#10442 : testing astype(str) is correct for Series/DatetimeIndex
41+
result = pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str)
42+
expected = pd.Series(
43+
['2012-01-01', '2012-01-02', '2012-01-03'], dtype=object)
44+
tm.assert_series_equal(result, expected)
45+
46+
result = Series(pd.date_range('2012-01-01', periods=3,
47+
tz='US/Eastern')).astype(str)
48+
expected = Series(['2012-01-01 00:00:00-05:00',
49+
'2012-01-02 00:00:00-05:00',
50+
'2012-01-03 00:00:00-05:00'],
51+
dtype=object)
52+
tm.assert_series_equal(result, expected)
53+
54+
def test_astype_str_compat(self):
55+
# GH 13149, GH 13209
56+
# verify that we are returing NaT as a string (and not unicode)
57+
58+
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
59+
result = idx.astype(str)
60+
expected = Index(['2016-05-16', 'NaT', 'NaT', 'NaT'], dtype=object)
61+
tm.assert_index_equal(result, expected)
62+
63+
def test_astype_str(self):
64+
# test astype string - #10442
65+
result = date_range('2012-01-01', periods=4,
66+
name='test_name').astype(str)
67+
expected = Index(['2012-01-01', '2012-01-02', '2012-01-03',
68+
'2012-01-04'], name='test_name', dtype=object)
69+
tm.assert_index_equal(result, expected)
70+
71+
# test astype string with tz and name
72+
result = date_range('2012-01-01', periods=3, name='test_name',
73+
tz='US/Eastern').astype(str)
74+
expected = Index(['2012-01-01 00:00:00-05:00',
75+
'2012-01-02 00:00:00-05:00',
76+
'2012-01-03 00:00:00-05:00'],
77+
name='test_name', dtype=object)
78+
tm.assert_index_equal(result, expected)
79+
80+
# test astype string with freqH and name
81+
result = date_range('1/1/2011', periods=3, freq='H',
82+
name='test_name').astype(str)
83+
expected = Index(['2011-01-01 00:00:00', '2011-01-01 01:00:00',
84+
'2011-01-01 02:00:00'],
85+
name='test_name', dtype=object)
86+
tm.assert_index_equal(result, expected)
87+
88+
# test astype string with freqH and timezone
89+
result = date_range('3/6/2012 00:00', periods=2, freq='H',
90+
tz='Europe/London', name='test_name').astype(str)
91+
expected = Index(['2012-03-06 00:00:00+00:00',
92+
'2012-03-06 01:00:00+00:00'],
93+
dtype=object, name='test_name')
94+
tm.assert_index_equal(result, expected)
95+
96+
def test_astype_datetime64(self):
97+
# GH 13149, GH 13209
98+
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
99+
100+
result = idx.astype('datetime64[ns]')
101+
tm.assert_index_equal(result, idx)
102+
self.assertFalse(result is idx)
103+
104+
result = idx.astype('datetime64[ns]', copy=False)
105+
tm.assert_index_equal(result, idx)
106+
self.assertTrue(result is idx)
107+
108+
idx_tz = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN], tz='EST')
109+
result = idx_tz.astype('datetime64[ns]')
110+
expected = DatetimeIndex(['2016-05-16 05:00:00', 'NaT', 'NaT', 'NaT'],
111+
dtype='datetime64[ns]')
112+
tm.assert_index_equal(result, expected)
113+
114+
def test_astype_raises(self):
115+
# GH 13149, GH 13209
116+
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
117+
118+
self.assertRaises(ValueError, idx.astype, float)
119+
self.assertRaises(ValueError, idx.astype, 'timedelta64')
120+
self.assertRaises(ValueError, idx.astype, 'timedelta64[ns]')
121+
self.assertRaises(ValueError, idx.astype, 'datetime64')
122+
self.assertRaises(ValueError, idx.astype, 'datetime64[D]')

0 commit comments

Comments
 (0)