Skip to content

Commit

Permalink
Add to_pandas method for Dataset and related test (#5247)
Browse files Browse the repository at this point in the history
Co-authored-by: Mathias Hauser <mathias.hauser@env.ethz.ch>
  • Loading branch information
gcaria and mathause authored May 4, 2021
1 parent 1c198a1 commit 4aef8f9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
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

0 comments on commit 4aef8f9

Please sign in to comment.