Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
from pandas.io.formats.format import _get_format_timedelta64

formatter = _get_format_timedelta64(self._data, na_rep)
return np.array([formatter(x) for x in self._data])
return np.array([formatter(x) for x in self._data.ravel()]).reshape(self.shape)

# ----------------------------------------------------------------
# Arithmetic Methods
Expand Down
22 changes: 3 additions & 19 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2366,26 +2366,10 @@ def fillna(self, value, **kwargs):
)
return super().fillna(value, **kwargs)

def to_native_types(self, na_rep=None, quoting=None, **kwargs):
def to_native_types(self, na_rep="NaT", **kwargs):
""" convert to our native types format """
values = self.values
mask = isna(values)

rvalues = np.empty(values.shape, dtype=object)
if na_rep is None:
na_rep = "NaT"
rvalues[mask] = na_rep
imask = (~mask).ravel()

# FIXME:
# should use the formats.format.Timedelta64Formatter here
# to figure what format to pass to the Timedelta
# e.g. to not show the decimals say
rvalues.flat[imask] = np.array(
[Timedelta(val)._repr_base(format="all") for val in values.ravel()[imask]],
dtype=object,
)
return rvalues
tda = self.array_values()
return tda._format_native_types(na_rep, **kwargs)


class BoolBlock(NumericBlock):
Expand Down
7 changes: 1 addition & 6 deletions pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,7 @@ def test_astype_str(self):
{
"a": list(map(str, map(lambda x: Timestamp(x)._date_repr, a._values))),
"b": list(map(str, map(Timestamp, b._values))),
"c": list(
map(
str,
map(lambda x: Timedelta(x)._repr_base(format="all"), c._values),
)
),
"c": list(map(lambda x: Timedelta(x)._repr_base(), c._values)),
"d": list(map(str, d._values)),
"e": list(map(str, e._values)),
}
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ def test_to_csv_timedelta_precision(self):
result = buf.getvalue()
expected_rows = [
",0",
"0,0 days 00:00:00.000000001",
"1,0 days 00:00:00.000000001",
"0,00:00:00.000000001",
"1,00:00:00.000000001",
]
expected = tm.convert_rows_list_to_csv_str(expected_rows)
assert result == expected
Expand Down
11 changes: 7 additions & 4 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_astype_str_map(self, dtype, series):
expected = series.map(str)
tm.assert_series_equal(result, expected)

def test_astype_str_cast(self):
def test_astype_str_cast_dt64(self):
# see gh-9757
ts = Series([Timestamp("2010-01-04 00:00:00")])
s = ts.astype(str)
Expand All @@ -146,11 +146,14 @@ def test_astype_str_cast(self):
expected = Series([str("2010-01-04 00:00:00-05:00")])
tm.assert_series_equal(s, expected)

def test_astype_str_cast_td64(self):
# see gh-9757

td = Series([Timedelta(1, unit="d")])
s = td.astype(str)
ser = td.astype(str)

expected = Series([str("1 days 00:00:00.000000000")])
tm.assert_series_equal(s, expected)
expected = Series([str("1 days")])
tm.assert_series_equal(ser, expected)

def test_astype_unicode(self):
# see gh-7758: A bit of magic is required to set
Expand Down