home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 542594876

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/issues/3407#issuecomment-542594876 https://api.github.com/repos/pydata/xarray/issues/3407 542594876 MDEyOklzc3VlQ29tbWVudDU0MjU5NDg3Ng== 868027 2019-10-16T08:44:50Z 2019-10-16T08:44:50Z CONTRIBUTOR

Hi @zxdawn

Does this modified version of your code do what you want?: python import numpy as np import xarray as xr tstr='2019-07-25_00:00:00' Times = xr.DataArray(np.array([tstr], dtype = np.dtype(('S', 16))), dims = ['Time']) ds = xr.Dataset({'Times':Times}) ds.to_netcdf( 'test.nc', format='NETCDF4', encoding={ 'Times': { 'zlib':True, 'complevel':5, 'char_dim_name':'DateStrLen' } }, unlimited_dims={'Time':True} ) Output of ncdump: ``` netcdf test { dimensions: Time = UNLIMITED ; // (1 currently) DateStrLen = 19 ; variables: char Times(Time, DateStrLen) ; data:

Times = "2019-07-25_00:00:00" ; } ```

Some explanation of what is going on: Strings in numpy aren't the most friendly thing to work with, and the data types can be a little confusing. In your code, the "S1" data type is saying "this array has null terminated strings of length 1". That 1 in the "S1" is the string length. This resulted in you having an array of one character strings that was 19 elements long: array([[b'2', b'0', b'1', b'9', b'-', b'0', b'7', b'-', b'2', b'5', b'_', b'0', b'0', b':', b'0', b'0', b':', b'0', b'0']], dtype='|S1') vs what I think you want: array([b'2019-07-25_00:00:00'], dtype='|S19')

Since you know that your string length is going to be 19, you should tell numpy about this instead of xarray by either specifying the data type as "S19" or using the data type constructor (which I prefer): np.dtype(("S", 19))

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