Skip to content

Formatting error in conjunction with pandas.DataFrame #2173

Closed

Description

@Hoeze

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()

Details /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

Activity

  1. shoyer commented on May 22, 2018

    @shoyer
    Member

    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.

  2. stale commented on Apr 23, 2020

    @stale

    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

  3. max-sixty commented on Apr 23, 2020

    @max-sixty
    Collaborator

    Closed by #2174

  4. keewis commented on Apr 23, 2020

    @keewis
    Collaborator

    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?

  5. reopened this on Apr 23, 2020
  6. keewis commented on Apr 23, 2020

    @keewis
    Collaborator

    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?

  7. keewis commented on Apr 13, 2021

    @keewis
    Collaborator

    let's continue this in #5148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions