Skip to content

Commit 9b842a0

Browse files
committed
Merge pull request pandas-dev#9758 from evanpw/issue_9757
BUG: allow conversion of Timestamp and Timedelta to string in astype
2 parents 5dff7df + b2fa8c3 commit 9b842a0

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Enhancements
3030
df = DataFrame(np.random.randn(3, 3), columns=['A', 'B', 'C'])
3131
df.drop(['A', 'X'], axis=1, errors='ignore')
3232

33+
- Allow conversion of values with dtype ``datetime64`` or ``timedelta64`` to strings using ``astype(str)`` (:issue:`9757`)
3334

3435
.. _whatsnew_0161.api:
3536

pandas/core/common.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,12 @@ def _astype_nansafe(arr, dtype, copy=True):
26372637
if not isinstance(dtype, np.dtype):
26382638
dtype = _coerce_to_dtype(dtype)
26392639

2640-
if is_datetime64_dtype(arr):
2640+
if issubclass(dtype.type, compat.text_type):
2641+
# in Py3 that's str, in Py2 that's unicode
2642+
return lib.astype_unicode(arr.ravel()).reshape(arr.shape)
2643+
elif issubclass(dtype.type, compat.string_types):
2644+
return lib.astype_str(arr.ravel()).reshape(arr.shape)
2645+
elif is_datetime64_dtype(arr):
26412646
if dtype == object:
26422647
return tslib.ints_to_pydatetime(arr.view(np.int64))
26432648
elif dtype == np.int64:
@@ -2675,11 +2680,6 @@ def _astype_nansafe(arr, dtype, copy=True):
26752680
elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.integer):
26762681
# work around NumPy brokenness, #1987
26772682
return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
2678-
elif issubclass(dtype.type, compat.text_type):
2679-
# in Py3 that's str, in Py2 that's unicode
2680-
return lib.astype_unicode(arr.ravel()).reshape(arr.shape)
2681-
elif issubclass(dtype.type, compat.string_types):
2682-
return lib.astype_str(arr.ravel()).reshape(arr.shape)
26832683

26842684
if copy:
26852685
return arr.astype(dtype)

pandas/tests/test_frame.py

+24
Original file line numberDiff line numberDiff line change
@@ -4192,6 +4192,30 @@ def test_astype_cast_nan_int(self):
41924192
df = DataFrame(data={"Values": [1.0, 2.0, 3.0, np.nan]})
41934193
self.assertRaises(ValueError, df.astype, np.int64)
41944194

4195+
def test_astype_str(self):
4196+
# GH9757
4197+
a = Series(date_range('2010-01-04', periods=5))
4198+
b = Series(date_range('3/6/2012 00:00', periods=5, tz='US/Eastern'))
4199+
c = Series([Timedelta(x, unit='d') for x in range(5)])
4200+
d = Series(range(5))
4201+
e = Series([0.0, 0.2, 0.4, 0.6, 0.8])
4202+
4203+
df = DataFrame({'a' : a, 'b' : b, 'c' : c, 'd' : d, 'e' : e})
4204+
4205+
# Test str and unicode on python 2.x and just str on python 3.x
4206+
for tt in set([str, compat.text_type]):
4207+
result = df.astype(tt)
4208+
4209+
expected = DataFrame({
4210+
'a' : list(map(tt, a.values)),
4211+
'b' : list(map(tt, b.values)),
4212+
'c' : list(map(tt, c.values)),
4213+
'd' : list(map(tt, d.values)),
4214+
'e' : list(map(tt, e.values)),
4215+
})
4216+
4217+
assert_frame_equal(result, expected)
4218+
41954219
def test_array_interface(self):
41964220
result = np.sqrt(self.frame)
41974221
tm.assert_isinstance(result, type(self.frame))

pandas/tests/test_series.py

+18
Original file line numberDiff line numberDiff line change
@@ -5511,6 +5511,24 @@ def test_astype_str(self):
55115511
expec = s.map(compat.text_type)
55125512
assert_series_equal(res, expec)
55135513

5514+
# GH9757
5515+
# Test str and unicode on python 2.x and just str on python 3.x
5516+
for tt in set([str, compat.text_type]):
5517+
ts = Series([Timestamp('2010-01-04 00:00:00')])
5518+
s = ts.astype(tt)
5519+
expected = Series([tt(ts.values[0])])
5520+
assert_series_equal(s, expected)
5521+
5522+
ts = Series([Timestamp('2010-01-04 00:00:00', tz='US/Eastern')])
5523+
s = ts.astype(tt)
5524+
expected = Series([tt(ts.values[0])])
5525+
assert_series_equal(s, expected)
5526+
5527+
td = Series([Timedelta(1, unit='d')])
5528+
s = td.astype(tt)
5529+
expected = Series([tt(td.values[0])])
5530+
assert_series_equal(s, expected)
5531+
55145532
def test_astype_unicode(self):
55155533

55165534
# GH7758

0 commit comments

Comments
 (0)