Skip to content
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

Formatting error in conjunction with pandas.DataFrame #2173

Closed
Hoeze opened this issue May 22, 2018 · 6 comments
Closed

Formatting error in conjunction with pandas.DataFrame #2173

Hoeze opened this issue May 22, 2018 · 6 comments
Labels

Comments

@Hoeze
Copy link

Hoeze commented May 22, 2018

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
import xarray as xr

sample_data = np.random.uniform(size=[2,2000,10000])
x = xr.Dataset({"sample_data": (sample_data.shape, sample_data)})
print(x)

df = pd.DataFrame({"x": [1,2,3], "y": [2,4,6]})
x["df"] = df
print(x)

grafik

Problem description

Printing a xarray.Dataset results in an error when containing a pandas.DataFrame

Expected Output

Should print string representation of Dataset

Output of xr.show_versions()

/usr/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.16.9-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8

xarray: 0.10.4
pandas: 0.22.0
numpy: 1.14.3
scipy: 1.0.1
netCDF4: None
h5netcdf: 0.5.1
h5py: 2.7.1
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: None
distributed: None
matplotlib: 2.2.2
cartopy: None
seaborn: None
setuptools: 39.2.0
pip: 10.0.1
conda: None
pytest: None
IPython: 6.3.1
sphinx: None

@shoyer
Copy link
Member

shoyer commented May 22, 2018

This is a manifestation of the same issue as #2172: you are using integers as dimension names, which we don't support very well.

In this case, you end up with mixed integers/strings as dimension names, which means our internal use of sorted() in repr(x) fails.

@stale
Copy link

stale bot commented Apr 23, 2020

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Apr 23, 2020
@max-sixty
Copy link
Collaborator

Closed by #2174

@stale stale bot removed the stale label Apr 23, 2020
@keewis
Copy link
Collaborator

keewis commented Apr 23, 2020

I can reproduce this on master so there's still more to do to fix this. Maybe we could try sorting using the str representation of the dimension names?

@keewis keewis reopened this Apr 23, 2020
@keewis
Copy link
Collaborator

keewis commented Apr 23, 2020

This works:

diff --git a/xarray/core/utils.py b/xarray/core/utils.py
index 1126cf30..355fb552 100644
--- a/xarray/core/utils.py
+++ b/xarray/core/utils.py
@@ -453,7 +453,7 @@ class SortedKeysDict(MutableMapping[K, V]):
         del self.mapping[key]
 
     def __iter__(self) -> Iterator[K]:
-        return iter(sorted(self.mapping))
+        return iter(sorted(self.mapping, key=str))
 
     def __len__(self) -> int:
         return len(self.mapping)

But maybe it is too naive?

@keewis
Copy link
Collaborator

keewis commented Apr 13, 2021

let's continue this in #5148

@keewis keewis closed this as completed Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants