Skip to content

REF: dispatch TDBlock.to_native_types to TDA._format_native_types #33270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def array(

>>> pd.array(["1H", "2H"], dtype='timedelta64[ns]')
<TimedeltaArray>
['01:00:00', '02:00:00']
['0 days 01:00:00', '0 days 02:00:00']
Length: 2, dtype: timedelta64[ns]

Examples
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ class TimedeltaProperties(Properties):
... pd.timedelta_range(start="1 second", periods=3, freq="S")
... )
>>> seconds_series
0 00:00:01
1 00:00:02
2 00:00:03
0 0 days 00:00:01
1 0 days 00:00:02
2 0 days 00:00:03
dtype: timedelta64[ns]
>>> seconds_series.dt.seconds
0 1
Expand Down Expand Up @@ -301,11 +301,11 @@ def components(self):
--------
>>> s = pd.Series(pd.to_timedelta(np.arange(5), unit='s'))
>>> s
0 00:00:00
1 00:00:01
2 00:00:02
3 00:00:03
4 00:00:04
0 0 days 00:00:00
1 0 days 00:00:01
2 0 days 00:00:02
3 0 days 00:00:03
4 0 days 00:00:04
dtype: timedelta64[ns]
>>> s.dt.components
days hours minutes seconds milliseconds microseconds nanoseconds
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 @@ -2358,26 +2358,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
4 changes: 2 additions & 2 deletions pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def to_timedelta(arg, unit="ns", errors="raise"):
Converting numbers by specifying the `unit` keyword argument:

>>> pd.to_timedelta(np.arange(5), unit='s')
TimedeltaIndex(['00:00:00', '00:00:01', '00:00:02',
'00:00:03', '00:00:04'],
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
'0 days 00:00:03', '0 days 00:00:04'],
dtype='timedelta64[ns]', freq=None)
>>> pd.to_timedelta(np.arange(5), unit='d')
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
Expand Down
5 changes: 0 additions & 5 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,14 +1672,9 @@ def _get_format_timedelta64(
even_days = (
np.logical_and(consider_values, values_int % one_day_nanos != 0).sum() == 0
)
all_sub_day = (
np.logical_and(consider_values, np.abs(values_int) >= one_day_nanos).sum() == 0
)

if even_days:
format = None
elif all_sub_day:
format = "sub_day"
else:
format = "long"

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
6 changes: 3 additions & 3 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3003,13 +3003,13 @@ def test_days_neg(self):
def test_subdays(self):
y = pd.to_timedelta(list(range(5)) + [pd.NaT], unit="s")
result = fmt.Timedelta64Formatter(y, box=True).get_result()
assert result[0].strip() == "'00:00:00'"
assert result[1].strip() == "'00:00:01'"
assert result[0].strip() == "'0 days 00:00:00'"
assert result[1].strip() == "'0 days 00:00:01'"

def test_subdays_neg(self):
y = pd.to_timedelta(list(range(5)) + [pd.NaT], unit="s")
result = fmt.Timedelta64Formatter(-y, box=True).get_result()
assert result[0].strip() == "'00:00:00'"
assert result[0].strip() == "'0 days 00:00:00'"
assert result[1].strip() == "'-1 days +23:59:59'"

def test_zero(self):
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