home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 833233473

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/pull/5264#issuecomment-833233473 https://api.github.com/repos/pydata/xarray/issues/5264 833233473 MDEyOklzc3VlQ29tbWVudDgzMzIzMzQ3Mw== 23625371 2021-05-06T05:21:19Z 2021-07-14T01:08:18Z CONTRIBUTOR

What problem are you trying to solve here?

Somehow I ended up with np.str_ in a pandas dataframe (how is unclear to me but this seems to be a valid string type), which then exploded when I converted to xarray and attempted to save as netCDF. Minimal example below.

```python import numpy as np import pandas as pd

I don't know how the strings ended up being np.str_....

scenarios = [np.str_(v) for v in ["scenario_a", "scenario_b", "scenario_c"]] years = range(2015, 2100 + 1) tdf = pd.DataFrame( data=np.random.random((len(scenarios), len(years))), columns=years, index=scenarios, ) tdf.index.name = "scenario" tdf.columns.name = "year" tdf = tdf.stack() tdf.name = "tas"

txr = tdf.to_xarray()

raises error shown below

txr.to_netcdf("test.nc")

error

Traceback (most recent call last): File "scratch.py", line 20, in <module> txr.to_netcdf("test.nc") File ".../lib/python3.7/site-packages/xarray/core/dataarray.py", line 2741, in to_netcdf return dataset.to_netcdf(args, *kwargs) File ".../lib/python3.7/site-packages/xarray/core/dataset.py", line 1699, in to_netcdf invalid_netcdf=invalid_netcdf, File ".../lib/python3.7/site-packages/xarray/backends/api.py", line 1108, in to_netcdf dataset, store, writer, encoding=encoding, unlimited_dims=unlimited_dims File ".../lib/python3.7/site-packages/xarray/backends/api.py", line 1154, in dump_to_store store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims) File ".../lib/python3.7/site-packages/xarray/backends/common.py", line 256, in store variables, check_encoding_set, writer, unlimited_dims=unlimited_dims File ".../lib/python3.7/site-packages/xarray/backends/common.py", line 294, in set_variables name, v, check, unlimited_dims=unlimited_dims File ".../lib/python3.7/site-packages/xarray/backends/netCDF4_.py", line 464, in prepare_variable variable, self.format, raise_on_invalid_encoding=check_encoding File ".../lib/python3.7/site-packages/xarray/backends/netCDF4_.py", line 131, in get_datatype datatype = _nc4_dtype(var) File ".../lib/python3.7/site-packages/xarray/backends/netCDF4.py", line 154, in _nc4_dtype raise ValueError(f"unsupported dtype for netCDF4 variable: {var.dtype}") ValueError: unsupported dtype for netCDF4 variable: object ```

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  877016428
Powered by Datasette · Queries took 0.544ms · About: xarray-datasette