Skip to content

tocsv_interval _categories #46562

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

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 4 additions & 0 deletions pandas/core/array_algos/take.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pandas.core.dtypes.common import (
ensure_platform_int,
is_1d_only_ea_obj,
is_interval_dtype,
)
from pandas.core.dtypes.missing import na_value_for_dtype

Expand Down Expand Up @@ -110,6 +111,9 @@ def take_nd(
return arr.take(
indexer, fill_value=fill_value, allow_fill=allow_fill, axis=axis
)
if arr.dtype.kind in "O" and is_interval_dtype(arr):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elif

# # GH46297 Interval
return arr.take(indexer, allow_fill=allow_fill)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill_value here comes from the na_rep argument of to_csv; is ignoring it the right behavior? Does that mean whatever the user specifies will be ignored?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, this seems like a weird place to special-case


return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,13 @@ def to_native_types(
results_converted.append(result.astype(object, copy=False))
return np.vstack(results_converted)

elif isinstance(values, (PeriodArray, IntervalArray)):
# GH46297
values = np.array(values, dtype="object")
mask = isna(values)
values[mask] = na_rep
return values
Comment on lines +2284 to +2289
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very similar to the EA block below (L2321); can they be combined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kyrpel can you do this one


elif values.dtype.kind == "f" and not is_sparse(values):
# see GH#13418: no special formatting is desired at the
# output (important for appropriate 'quoting' behaviour),
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ def test_to_csv_date_format_in_categorical(self):
ser = ser.astype("category")
assert ser.to_csv(index=False, date_format="%Y-%m-%d") == expected

def test_to_cvs_interval_format_in_categorical(self):
# GH#46297
df = DataFrame(index=[0], columns=["a"])
df.at[0, "a"] = pd.Interval(
pd.Timestamp("2020-01-01"), pd.Timestamp("2020-01-02")
)
df["a"] = df["a"].astype("category")
result = df.to_csv(index=False, date_format="%Y-%m-%d")
expected = tm.convert_rows_list_to_csv_str(["a", '"(2020-01-01, 2020-01-02]"'])
assert result == expected

def test_to_csv_float_ea_float_format(self):
# GH#45991
df = DataFrame({"a": [1.1, 2.02, pd.NA, 6.000006], "b": "c"})
Expand Down