|
11 | 11 | from pandas import (Index, Series, TimeSeries, DataFrame, isnull,
|
12 | 12 | date_range, Timestamp)
|
13 | 13 |
|
14 |
| -from pandas import DatetimeIndex |
| 14 | +from pandas import DatetimeIndex, to_datetime |
15 | 15 |
|
16 | 16 | from pandas.core.daterange import DateRange
|
17 | 17 |
|
@@ -111,33 +111,6 @@ def assert_range_equal(left, right):
|
111 | 111 |
|
112 | 112 | class TestTimeSeries(unittest.TestCase):
|
113 | 113 |
|
114 |
| - def test_string_na_conversion(self): |
115 |
| - from dateutil.parser import parse |
116 |
| - from pandas.core.datetools import to_datetime |
117 |
| - |
118 |
| - strings = np.array(['1/1/2000', '1/2/2000', np.nan, |
119 |
| - '1/4/2000, 12:34:56'], dtype=object) |
120 |
| - |
121 |
| - expected = [] |
122 |
| - for val in strings: |
123 |
| - if com.isnull(val): |
124 |
| - expected.append(val) |
125 |
| - else: |
126 |
| - expected.append(parse(val)) |
127 |
| - |
128 |
| - result = lib.string_to_datetime(strings) |
129 |
| - assert_almost_equal(result, expected) |
130 |
| - |
131 |
| - result2 = to_datetime(strings) |
132 |
| - assert_almost_equal(result, result2) |
133 |
| - |
134 |
| - malformed = np.array(['1/100/2000', np.nan], dtype=object) |
135 |
| - result = to_datetime(malformed) |
136 |
| - assert_almost_equal(result, malformed) |
137 |
| - |
138 |
| - self.assertRaises(ValueError, to_datetime, malformed, |
139 |
| - errors='raise') |
140 |
| - |
141 | 114 | def test_dti_slicing(self):
|
142 | 115 | dti = DatetimeIndex(start='1/1/2005', end='12/1/2005', freq='M')
|
143 | 116 | dti2 = dti[[1,3,5]]
|
@@ -349,6 +322,59 @@ def test_fillna_nat(self):
|
349 | 322 | assert_frame_equal(filled, expected)
|
350 | 323 | assert_frame_equal(filled2, expected)
|
351 | 324 |
|
| 325 | + def test_string_na_nat_conversion(self): |
| 326 | + # GH #999, #858 |
| 327 | + |
| 328 | + from dateutil.parser import parse |
| 329 | + from pandas.core.datetools import to_datetime |
| 330 | + |
| 331 | + strings = np.array(['1/1/2000', '1/2/2000', np.nan, |
| 332 | + '1/4/2000, 12:34:56'], dtype=object) |
| 333 | + |
| 334 | + expected = np.empty(4, dtype='M8') |
| 335 | + for i, val in enumerate(strings): |
| 336 | + if com.isnull(val): |
| 337 | + expected[i] = NaT |
| 338 | + else: |
| 339 | + expected[i] = parse(val) |
| 340 | + |
| 341 | + result = lib.string_to_datetime(strings) |
| 342 | + assert_almost_equal(result, expected) |
| 343 | + |
| 344 | + result2 = to_datetime(strings) |
| 345 | + assert_almost_equal(result, result2) |
| 346 | + |
| 347 | + malformed = np.array(['1/100/2000', np.nan], dtype=object) |
| 348 | + result = to_datetime(malformed) |
| 349 | + assert_almost_equal(result, malformed) |
| 350 | + |
| 351 | + self.assertRaises(ValueError, to_datetime, malformed, |
| 352 | + errors='raise') |
| 353 | + |
| 354 | + idx = ['a', 'b', 'c', 'd', 'e'] |
| 355 | + series = Series(['1/1/2000', np.nan, '1/3/2000', np.nan, |
| 356 | + '1/5/2000'], index=idx, name='foo') |
| 357 | + dseries = Series([to_datetime('1/1/2000'), np.nan, |
| 358 | + to_datetime('1/3/2000'), np.nan, |
| 359 | + to_datetime('1/5/2000')], index=idx, name='foo') |
| 360 | + |
| 361 | + result = to_datetime(series) |
| 362 | + dresult = to_datetime(dseries) |
| 363 | + |
| 364 | + expected = Series(np.empty(5, dtype='M8[us]'), index=idx) |
| 365 | + for i in range(5): |
| 366 | + x = series[i] |
| 367 | + if isnull(x): |
| 368 | + expected[i] = NaT |
| 369 | + else: |
| 370 | + expected[i] = to_datetime(x) |
| 371 | + |
| 372 | + assert_series_equal(result, expected) |
| 373 | + self.assertEquals(result.name, 'foo') |
| 374 | + |
| 375 | + assert_series_equal(dresult, expected) |
| 376 | + self.assertEquals(dresult.name, 'foo') |
| 377 | + |
352 | 378 | def _skip_if_no_pytz():
|
353 | 379 | try:
|
354 | 380 | import pytz
|
|
0 commit comments