Conversation
Recognize numpy.dtypes.StringDType (kind "T") as a unicode string type in is_unicode_dtype, and convert StringDType arrays to object arrays before passing to netCDF4/h5netcdf backends which don't support StringDType natively. Null values from StringDType(na_object=None) are replaced with empty strings on write. Co-authored-by: Claude <noreply@anthropic.com>
- Handle StringDType null values in encode_string_array (scipy/nc3 path) - Add roundtrip tests for StringDType with na_object=None and na_object="" - Add unit test for encode_string_array with StringDType nulls Co-Authored-By: Claude <noreply@anthropic.com>
|
Thanks for the review @jsignell ! I looked into scipy and the situation is a bit different there. The scipy backend goes through I added these changes:
All roundtrip tests are in |
jsignell
left a comment
There was a problem hiding this comment.
It looks like this is getting close. I really like the new tests. Since tests are passing now on CI it does look like you have some failures in yours (https://github.com/pydata/xarray/actions/runs/23211060740/job/67459709862?pr=11218#step:10:42). I am suspicious about whether this string_array[string_array == None] = "" is doing quite the right thing. Does == None accurately capture the null value even if na_object is set?
|
I realized that unlike converting StringDType to numpy 'object' dtype, converting to fixed-width unicode ( |
Convert StringDType to fixed-width unicode (U) in EncodedStringCoder.encode() instead of per-backend prepare_variable, fixing Zarr and CFEncodedDataStore. Co-authored-by: Claude <noreply@anthropic.com>
* Allow writing StringDType variables to netCDF (pydata#11199) Recognize numpy.dtypes.StringDType (kind "T") as a unicode string type in is_unicode_dtype, and convert StringDType arrays to object arrays before passing to netCDF4/h5netcdf backends which don't support StringDType natively. Null values from StringDType(na_object=None) are replaced with empty strings on write. Co-authored-by: Claude <noreply@anthropic.com> * Add StringDType null handling for scipy and tests for review feedback - Handle StringDType null values in encode_string_array (scipy/nc3 path) - Add roundtrip tests for StringDType with na_object=None and na_object="" - Add unit test for encode_string_array with StringDType nulls Co-Authored-By: Claude <noreply@anthropic.com> * Move StringDType handling into shared encoder (pydata#11199) Convert StringDType to fixed-width unicode (U) in EncodedStringCoder.encode() instead of per-backend prepare_variable, fixing Zarr and CFEncodedDataStore. Co-authored-by: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Julia Signell <jsignell@gmail.com>
The evaluator integration test on beaker failed with the following
error:
<details>
<p>
```
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/opt/conda/lib/python3.11/site-packages/fme/ace/evaluator.py", line 9,
in <module>
main(args.yaml_config, override_dotlist=args.override)
File "/opt/conda/lib/python3.11/site-packages/fme/ace/inference/evaluator.py",
line 289, in main
return run_evaluator_from_config(config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/fme/ace/inference/evaluator.py",
line 459, in run_evaluator_from_config
aggregator.flush_diagnostics()
File "/opt/conda/lib/python3.11/site-packages/torch/utils/_contextlib.py",
line 116, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File
"/opt/conda/lib/python3.11/site-packages/fme/ace/aggregator/inference/main.py",
line 573, in flush_diagnostics
write_reduced_diagnostics(
File "/opt/conda/lib/python3.11/site-packages/fme/core/diagnostics.py", line
60, in write_reduced_diagnostics
to_netcdf_via_inter_filesystem_copy(ds, filename)
File "/opt/conda/lib/python3.11/site-packages/fme/core/cloud.py", line 38, in
to_netcdf_via_inter_filesystem_copy
ds.to_netcdf(source)
File "/opt/conda/lib/python3.11/site-packages/xarray/core/dataset.py", line
2123, in to_netcdf
return to_netcdf( # type: ignore[return-value] # mypy cannot resolve the
overloads:(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/writers.py",
line 441, in to_netcdf
dump_to_store(
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/writers.py",
line 491, in dump_to_store
store.store(variables, attrs, check_encoding, writer,
unlimited_dims=unlimited_dims)
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/common.py", line
533, in store
self.set_variables(
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/common.py", line
571, in set_variables
target, source = self.prepare_variable(
^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/netCDF4_.py",
line 633, in prepare_variable
datatype = _get_datatype(
^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/netCDF4_.py",
line 165, in _get_datatype
return _nc4_dtype(var)
^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/xarray/backends/netCDF4_.py",
line 186, in _nc4_dtype
raise ValueError(f"unsupported dtype for netCDF4 variable: {var.dtype}")
ValueError: unsupported dtype for netCDF4 variable: StringDType(na_object=nan)
```
</p>
</details>
This should be fixed in the next release of xarray
(pydata/xarray#11218), but for the time being we
can work around it with the code in this PR.
Changes:
- Modifies the way we assign `"source"` coordinate values to the dataset
constructed in `PairedSphericalPowerSpectrumAggregator.get_dataset` such
that they are of a unicode dtype rather than a string dtype.
- [x] Tests added
Summary
numpy.dtypes.StringDType(kind"T") as a unicode string type inis_unicode_dtype, so the encoding pipeline and backend dtype selection handle it correctly.netCDF4andh5netcdfbackendprepare_variablemethods, since neither C library supports StringDType natively.StringDType(na_object=None)are replaced with empty strings on write, matching existing behavior for object-dtype string arrays with missing values.EncodedStringCoder(allows_unicode=False)encodes strings to bytes viaencode_string_array, which handles StringDType.Test plan
test_is_unicode_dtype_stringdtype— unit test foris_unicode_dtypewith StringDTypetest_roundtrip_stringdtype_data— roundtrip test inDatasetIOBase, runs across all backends (netCDF4, h5netcdf, scipy, zarr)StringDType(na_object=None)🤖 Generated with Claude Code