Skip to content

Commit ca6abdb

Browse files
committed
Try fix
1 parent 3f286a9 commit ca6abdb

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

xarray/coding/strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def check_vlen_dtype(dtype):
2929

3030

3131
def is_unicode_dtype(dtype):
32-
return dtype.kind == "U" or check_vlen_dtype(dtype) == str
32+
return dtype.kind == "U" or check_vlen_dtype(dtype) == str or check_vlen_dtype(dtype) == np.str_
3333

3434

3535
def is_bytes_dtype(dtype):

xarray/tests/test_coding_strings.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import suppress
22

33
import numpy as np
4+
import pandas as pd
45
import pytest
56

67
from xarray import Variable
@@ -32,10 +33,30 @@ def test_vlen_dtype():
3233
@pytest.mark.parametrize("numpy_str_type", (np.str, np.str_))
3334
def test_numpy_str_handling(numpy_str_type):
3435
dtype = strings.create_vlen_dtype(numpy_str_type)
35-
assert dtype.metadata["element_type"] == str
36+
assert dtype.metadata["element_type"] == numpy_str_type
3637
assert strings.is_unicode_dtype(dtype)
3738
assert not strings.is_bytes_dtype(dtype)
38-
assert strings.check_vlen_dtype(dtype) is str
39+
assert strings.check_vlen_dtype(dtype) is numpy_str_type
40+
41+
42+
@pytest.mark.parametrize("numpy_str_type", (np.str, np.str_))
43+
def test_write_file_from_np_str(numpy_str_type):
44+
# should be moved elsewhere probably
45+
scenarios = [numpy_str_type(v) for v in ["scenario_a", "scenario_b", "scenario_c"]]
46+
years = range(2015, 2100 + 1)
47+
tdf = pd.DataFrame(
48+
data=np.random.random((len(scenarios), len(years))),
49+
columns=years,
50+
index=scenarios,
51+
)
52+
tdf.index.name = "scenario"
53+
tdf.columns.name = "year"
54+
tdf = tdf.stack()
55+
tdf.name = "tas"
56+
57+
txr = tdf.to_xarray()
58+
59+
txr.to_netcdf("test.nc")
3960

4061

4162
def test_EncodedStringCoder_decode():

0 commit comments

Comments
 (0)