Skip to content

Add to_pandas method for Dataset #5247

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 2 commits into from
May 4, 2021
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
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ Dataset methods
open_rasterio
open_zarr
Dataset.to_netcdf
Dataset.to_pandas
Dataset.to_zarr
save_mfdataset
Dataset.to_array
Expand Down
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ v0.17.1 (unreleased)
New Features
~~~~~~~~~~~~

- Add :py:meth:`Dataset.to_pandas` (:pull:`5247`)
By `Giacomo Caria <https://github.com/gcaria>`_.
- Add :py:meth:`DataArray.plot.surface` which wraps matplotlib's `plot_surface` to make
surface plots (:issue:`#2235` :issue:`#5084` :pull:`5101`).
- Allow passing multiple arrays to :py:meth:`Dataset.__setitem__` (:pull:`5216`).
Expand Down
21 changes: 21 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5093,6 +5093,27 @@ def _normalize_dim_order(

return ordered_dims

def to_pandas(self) -> Union[pd.Series, pd.DataFrame]:
"""Convert this dataset into a pandas object without changing the number of dimensions.

The type of the returned object depends on the number of Dataset
dimensions:

* 0D -> `pandas.Series`
* 1D -> `pandas.DataFrame`

Only works for Datasets with 1 or fewer dimensions.
"""
if len(self.dims) == 0:
return pd.Series({k: v.item() for k, v in self.items()})
if len(self.dims) == 1:
return self.to_dataframe()
raise ValueError(
"cannot convert Datasets with %s dimensions into "
"pandas objects without changing the number of dimensions. "
"Please use Dataset.to_dataframe() instead." % len(self.dims)
)

def _to_dataframe(self, ordered_dims: Mapping[Hashable, int]):
columns = [k for k in self.variables if k not in self.dims]
data = [
Expand Down
21 changes: 21 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,27 @@ def test_broadcast_like(self):

assert_identical(original2.broadcast_like(original1), expected2)

def test_to_pandas(self):
# 0D -> series
actual = Dataset({"a": 1, "b": 2}).to_pandas()
expected = pd.Series([1, 2], ["a", "b"])
assert_array_equal(actual, expected)

# 1D -> dataframe
x = np.random.randn(10)
y = np.random.randn(10)
t = list("abcdefghij")
ds = Dataset({"a": ("t", x), "b": ("t", y), "t": ("t", t)})
actual = ds.to_pandas()
expected = ds.to_dataframe()
assert expected.equals(actual), (expected, actual)

# 2D -> error
x2d = np.random.randn(10, 10)
y2d = np.random.randn(10, 10)
with pytest.raises(ValueError, match=r"cannot convert Datasets"):
Dataset({"a": (["t", "r"], x2d), "b": (["t", "r"], y2d)}).to_pandas()

def test_reindex_like(self):
data = create_test_data()
data["letters"] = ("dim3", 10 * ["a"])
Expand Down