|
12 | 12 |
|
13 | 13 | class TestSeriesInternals: |
14 | 14 |
|
15 | | - def test_convert_objects(self): |
16 | | - |
17 | | - s = Series([1., 2, 3], index=['a', 'b', 'c']) |
18 | | - with tm.assert_produces_warning(FutureWarning): |
19 | | - result = s.convert_objects(convert_dates=False, |
20 | | - convert_numeric=True) |
21 | | - assert_series_equal(result, s) |
22 | | - |
23 | | - # force numeric conversion |
24 | | - r = s.copy().astype('O') |
25 | | - r['a'] = '1' |
26 | | - with tm.assert_produces_warning(FutureWarning): |
27 | | - result = r.convert_objects(convert_dates=False, |
28 | | - convert_numeric=True) |
29 | | - assert_series_equal(result, s) |
30 | | - |
31 | | - r = s.copy().astype('O') |
32 | | - r['a'] = '1.' |
33 | | - with tm.assert_produces_warning(FutureWarning): |
34 | | - result = r.convert_objects(convert_dates=False, |
35 | | - convert_numeric=True) |
36 | | - assert_series_equal(result, s) |
37 | | - |
38 | | - r = s.copy().astype('O') |
39 | | - r['a'] = 'garbled' |
40 | | - expected = s.copy() |
41 | | - expected['a'] = np.nan |
42 | | - with tm.assert_produces_warning(FutureWarning): |
43 | | - result = r.convert_objects(convert_dates=False, |
44 | | - convert_numeric=True) |
45 | | - assert_series_equal(result, expected) |
46 | | - |
47 | | - # GH 4119, not converting a mixed type (e.g.floats and object) |
48 | | - s = Series([1, 'na', 3, 4]) |
49 | | - with tm.assert_produces_warning(FutureWarning): |
50 | | - result = s.convert_objects(convert_numeric=True) |
51 | | - expected = Series([1, np.nan, 3, 4]) |
52 | | - assert_series_equal(result, expected) |
53 | | - |
54 | | - s = Series([1, '', 3, 4]) |
55 | | - with tm.assert_produces_warning(FutureWarning): |
56 | | - result = s.convert_objects(convert_numeric=True) |
57 | | - expected = Series([1, np.nan, 3, 4]) |
58 | | - assert_series_equal(result, expected) |
59 | | - |
60 | | - # dates |
61 | | - s = Series([datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 2, 0, 0), |
62 | | - datetime(2001, 1, 3, 0, 0)]) |
63 | | - s2 = Series([datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 2, 0, 0), |
64 | | - datetime(2001, 1, 3, 0, 0), 'foo', 1.0, 1, |
65 | | - Timestamp('20010104'), '20010105'], |
66 | | - dtype='O') |
67 | | - with tm.assert_produces_warning(FutureWarning): |
68 | | - result = s.convert_objects(convert_dates=True, |
69 | | - convert_numeric=False) |
70 | | - expected = Series([Timestamp('20010101'), Timestamp('20010102'), |
71 | | - Timestamp('20010103')], dtype='M8[ns]') |
72 | | - assert_series_equal(result, expected) |
73 | | - |
74 | | - with tm.assert_produces_warning(FutureWarning): |
75 | | - result = s.convert_objects(convert_dates='coerce', |
76 | | - convert_numeric=False) |
77 | | - with tm.assert_produces_warning(FutureWarning): |
78 | | - result = s.convert_objects(convert_dates='coerce', |
79 | | - convert_numeric=True) |
80 | | - assert_series_equal(result, expected) |
81 | | - |
82 | | - expected = Series([Timestamp('20010101'), Timestamp('20010102'), |
83 | | - Timestamp('20010103'), |
84 | | - NaT, NaT, NaT, Timestamp('20010104'), |
85 | | - Timestamp('20010105')], dtype='M8[ns]') |
86 | | - with tm.assert_produces_warning(FutureWarning): |
87 | | - result = s2.convert_objects(convert_dates='coerce', |
88 | | - convert_numeric=False) |
89 | | - assert_series_equal(result, expected) |
90 | | - with tm.assert_produces_warning(FutureWarning): |
91 | | - result = s2.convert_objects(convert_dates='coerce', |
92 | | - convert_numeric=True) |
93 | | - assert_series_equal(result, expected) |
94 | | - |
95 | | - # preserver all-nans (if convert_dates='coerce') |
96 | | - s = Series(['foo', 'bar', 1, 1.0], dtype='O') |
97 | | - with tm.assert_produces_warning(FutureWarning): |
98 | | - result = s.convert_objects(convert_dates='coerce', |
99 | | - convert_numeric=False) |
100 | | - expected = Series([NaT] * 2 + [Timestamp(1)] * 2) |
101 | | - assert_series_equal(result, expected) |
102 | | - |
103 | | - # preserver if non-object |
104 | | - s = Series([1], dtype='float32') |
105 | | - with tm.assert_produces_warning(FutureWarning): |
106 | | - result = s.convert_objects(convert_dates='coerce', |
107 | | - convert_numeric=False) |
108 | | - assert_series_equal(result, s) |
109 | | - |
110 | | - # r = s.copy() |
111 | | - # r[0] = np.nan |
112 | | - # result = r.convert_objects(convert_dates=True,convert_numeric=False) |
113 | | - # assert result.dtype == 'M8[ns]' |
114 | | - |
115 | | - # dateutil parses some single letters into today's value as a date |
116 | | - for x in 'abcdefghijklmnopqrstuvwxyz': |
117 | | - s = Series([x]) |
118 | | - with tm.assert_produces_warning(FutureWarning): |
119 | | - result = s.convert_objects(convert_dates='coerce') |
120 | | - assert_series_equal(result, s) |
121 | | - s = Series([x.upper()]) |
122 | | - with tm.assert_produces_warning(FutureWarning): |
123 | | - result = s.convert_objects(convert_dates='coerce') |
124 | | - assert_series_equal(result, s) |
125 | | - |
126 | | - def test_convert_objects_preserve_bool(self): |
127 | | - s = Series([1, True, 3, 5], dtype=object) |
128 | | - with tm.assert_produces_warning(FutureWarning): |
129 | | - r = s.convert_objects(convert_numeric=True) |
130 | | - e = Series([1, 1, 3, 5], dtype='i8') |
131 | | - tm.assert_series_equal(r, e) |
132 | | - |
133 | | - def test_convert_objects_preserve_all_bool(self): |
134 | | - s = Series([False, True, False, False], dtype=object) |
135 | | - with tm.assert_produces_warning(FutureWarning): |
136 | | - r = s.convert_objects(convert_numeric=True) |
137 | | - e = Series([False, True, False, False], dtype=bool) |
138 | | - tm.assert_series_equal(r, e) |
139 | | - |
140 | 15 | # GH 10265 |
141 | 16 | def test_convert(self): |
142 | 17 | # Tests: All to nans, coerce, true |
|
0 commit comments