Skip to content

Commit 472e6e0

Browse files
committed
Merge pull request #11148 from hamedhsn/bug#10442
BUG: astype(str) on datetimelike index #10442
2 parents b1e8129 + c2f102f commit 472e6e0

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Highlights include:
1717

1818
Enhancements
1919
~~~~~~~~~~~~
20+
- ``DatetimeIndex`` now supports conversion to strings with astype(str)(:issue:`10442`)
2021

2122
- Support for ``compression`` (gzip/bz2) in :method:`DataFrame.to_csv` (:issue:`7615`)
2223

pandas/tseries/index.py

+2
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ def astype(self, dtype):
756756
return self.asi8.copy()
757757
elif dtype == _NS_DTYPE and self.tz is not None:
758758
return self.tz_convert('UTC').tz_localize(None)
759+
elif dtype == str:
760+
return self._shallow_copy(values=self.format(), infer=True)
759761
else: # pragma: no cover
760762
raise ValueError('Cannot cast DatetimeIndex to dtype %s' % dtype)
761763

pandas/tseries/tests/test_base.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ def test_ops_properties_basic(self):
4545
self.assertEqual(s.day,10)
4646
self.assertRaises(AttributeError, lambda : s.weekday)
4747

48+
def test_astype_str(self):
49+
# test astype string - #10442
50+
result = date_range('2012-01-01', periods=4, name='test_name').astype(str)
51+
expected = Index(['2012-01-01', '2012-01-02', '2012-01-03','2012-01-04'],
52+
name='test_name', dtype=object)
53+
tm.assert_index_equal(result, expected)
54+
55+
# test astype string with tz and name
56+
result = date_range('2012-01-01', periods=3, name='test_name', tz='US/Eastern').astype(str)
57+
expected = Index(['2012-01-01 00:00:00-05:00', '2012-01-02 00:00:00-05:00',
58+
'2012-01-03 00:00:00-05:00'], name='test_name', dtype=object)
59+
tm.assert_index_equal(result, expected)
60+
61+
# test astype string with freqH and name
62+
result = date_range('1/1/2011', periods=3, freq='H', name='test_name').astype(str)
63+
expected = Index(['2011-01-01 00:00:00', '2011-01-01 01:00:00', '2011-01-01 02:00:00'],
64+
name='test_name', dtype=object)
65+
tm.assert_index_equal(result, expected)
66+
67+
# test astype string with freqH and timezone
68+
result = date_range('3/6/2012 00:00', periods=2, freq='H',
69+
tz='Europe/London', name='test_name').astype(str)
70+
expected = Index(['2012-03-06 00:00:00+00:00', '2012-03-06 01:00:00+00:00'],
71+
dtype=object, name='test_name')
72+
tm.assert_index_equal(result, expected)
73+
4874
def test_asobject_tolist(self):
4975
idx = pd.date_range(start='2013-01-01', periods=4, freq='M', name='idx')
5076
expected_list = [pd.Timestamp('2013-01-31'), pd.Timestamp('2013-02-28'),
@@ -503,7 +529,6 @@ def test_infer_freq(self):
503529
tm.assert_index_equal(idx, result)
504530
self.assertEqual(result.freq, freq)
505531

506-
507532
class TestTimedeltaIndexOps(Ops):
508533

509534
def setUp(self):

pandas/tseries/tests/test_timeseries.py

+12
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,7 @@ def test_append_join_nondatetimeindex(self):
22232223
# it works
22242224
rng.join(idx, how='outer')
22252225

2226+
22262227
def test_astype(self):
22272228
rng = date_range('1/1/2000', periods=10)
22282229

@@ -2235,6 +2236,17 @@ def test_astype(self):
22352236
expected = date_range('1/1/2000', periods=10, tz='US/Eastern').tz_convert('UTC').tz_localize(None)
22362237
tm.assert_index_equal(result, expected)
22372238

2239+
# BUG#10442 : testing astype(str) is correct for Series/DatetimeIndex
2240+
result = pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str)
2241+
expected = pd.Series(['2012-01-01', '2012-01-02', '2012-01-03'], dtype=object)
2242+
tm.assert_series_equal(result, expected)
2243+
2244+
result = Series(pd.date_range('2012-01-01', periods=3, tz='US/Eastern')).astype(str)
2245+
expected = Series(['2012-01-01 00:00:00-05:00', '2012-01-02 00:00:00-05:00', '2012-01-03 00:00:00-05:00'],
2246+
dtype=object)
2247+
tm.assert_series_equal(result, expected)
2248+
2249+
22382250
def test_to_period_nofreq(self):
22392251
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
22402252
self.assertRaises(ValueError, idx.to_period)

0 commit comments

Comments
 (0)