Skip to content

Commit 4f32b93

Browse files
committed
REGR: change in output formatting for long floats/nan, pandas-dev#11302
1 parent c2aa6a2 commit 4f32b93

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.17.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ API changes
3434

3535
- min and max reductions on ``datetime64`` and ``timedelta64`` dtyped series now
3636
result in ``NaT`` and not ``nan`` (:issue:`11245`).
37-
37+
- Regression from 0.16.2 for output formatting of long floats/nan, restored in (:issue:`11302`)
3838
- Prettyprinting sets (e.g. in DataFrame cells) now uses set literal syntax (``{x, y}``) instead of
3939
Legacy Python syntax (``set([x, y])``) (:issue:`11215`)
4040

pandas/core/internals.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,15 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
435435
if values is None:
436436

437437
if issubclass(dtype.type, (compat.text_type, compat.string_types)):
438-
values = self.to_native_types()
438+
439+
# use native type formatting for datetime/tz/timedelta
440+
if self.is_datelike:
441+
values = self.to_native_types()
442+
443+
# astype formatting
444+
else:
445+
values = self.values
446+
439447
else:
440448
values = self.get_values(dtype=dtype)
441449

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -4621,6 +4621,7 @@ def test_astype_str(self):
46214621

46224622
df = DataFrame({'a' : a, 'b' : b, 'c' : c, 'd' : d, 'e' : e})
46234623

4624+
# datetimelike
46244625
# Test str and unicode on python 2.x and just str on python 3.x
46254626
for tt in set([str, compat.text_type]):
46264627
result = df.astype(tt)
@@ -4635,6 +4636,18 @@ def test_astype_str(self):
46354636

46364637
assert_frame_equal(result, expected)
46374638

4639+
# float/nan
4640+
# 11302
4641+
# consistency in astype(str)
4642+
for tt in set([str, compat.text_type]):
4643+
result = DataFrame([np.NaN]).astype(tt)
4644+
expected = DataFrame(['nan'])
4645+
assert_frame_equal(result, expected)
4646+
4647+
result = DataFrame([1.12345678901234567890]).astype(tt)
4648+
expected = DataFrame(['1.12345678901'])
4649+
assert_frame_equal(result, expected)
4650+
46384651
def test_array_interface(self):
46394652
result = np.sqrt(self.frame)
46404653
tm.assertIsInstance(result, type(self.frame))

0 commit comments

Comments
 (0)