question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Differences on datetime values appears after writing reindexed variable on netCDF file

See original GitHub issue

In my Dataset i’ve got a time serie coordinate who begins like this

<xarray.DataArray 'time' (time: 10)>
array(['2014-02-15T00:00:00.000000000+0100',
       '2014-02-15T18:10:00.000000000+0100',
       '2014-02-16T18:10:00.000000000+0100',
       '2014-02-17T18:10:00.000000000+0100',
       '2014-02-18T18:10:00.000000000+0100',
       '2014-02-19T18:10:00.000000000+0100',
       '2014-02-20T18:10:00.000000000+0100',
       '2014-02-21T18:10:00.000000000+0100',
       '2014-02-22T00:00:00.000000000+0100',
       '2014-02-23T00:00:00.000000000+0100'], dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2014-02-14T23:00:00 2014-02-15T17:10:00 ...

And all is ok when I write and re-open the netdcdf file

Then i try to add to this dataset a reindexed variable like this

da["MeanRainfallHeigh"] = rain.reindex(time =da.time).fillna(0)

Everything is still good for the writing, but when I reopen the netcdf file, the time values are modified for the minutes part.

<xarray.DataArray 'time' (time: 10)>
array(['2014-02-15T00:00:00.000000000+0100',
       '2014-02-15T18:00:00.000000000+0100',
       '2014-02-16T18:00:00.000000000+0100',
       '2014-02-17T18:00:00.000000000+0100',
       '2014-02-18T18:00:00.000000000+0100',
       '2014-02-19T18:00:00.000000000+0100',
       '2014-02-20T18:00:00.000000000+0100',
       '2014-02-21T18:00:00.000000000+0100',
       '2014-02-22T00:00:00.000000000+0100',
       '2014-02-23T00:00:00.000000000+0100'], dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2014-02-14T23:00:00 2014-02-15T17:00:00 ...

Thanks!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
NotSqrtcommented, Jan 23, 2018

FYI, merged.time.encoding = {} before calling to_netcdf seems to avoid the RuntimeWarning.

0reactions
NotSqrtcommented, Jan 18, 2018

There you go !

import numpy
import pandas
import tempfile
import warnings
import xarray


array1 = xarray.DataArray(
    numpy.random.rand(5),
    dims=['time'],
    coords={'time': pandas.to_datetime(['2018-01-01', '2018-01-01 00:01', '2018-01-01 00:02', '2018-01-01 00:03', '2018-01-01 00:04'])},
    name='foo'
)

array2 = xarray.DataArray(
    numpy.random.rand(5),
    dims=['time'],
    coords={'time': pandas.to_datetime(['2018-01-01 00:05', '2018-01-01 00:05:10', '2018-01-01 00:05:20', '2018-01-01 00:05:30', '2018-01-01 00:05:40'])},
    name='foo'
)

with tempfile.NamedTemporaryFile() as tmp:
    # save first array
    array1.to_netcdf(tmp.name)
    # reload it
    array1_reloaded = xarray.open_dataarray(tmp.name)

    # the time encoding stores minutes as int, so seconds won't be allowed at next call of to_netcdf
    assert array1_reloaded.time.encoding['dtype'] == numpy.int64
    assert array1_reloaded.time.encoding['units'] == 'minutes since 2018-01-01 00:00:00'

    merged = xarray.merge([array1_reloaded, array2])
    array1_reloaded.close()

    with warnings.catch_warnings():
        warnings.filterwarnings('error', category=RuntimeWarning)
        merged.to_netcdf(tmp.name)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing files - Xarray
It is possible to append or overwrite netCDF variables using the mode='a' argument. When using this option, all variables in the dataset will...
Read more >
Writing NetCDF Files: Best Practices
There are two strategies for storing a date/time into a netCDF variable. One is to encode it as a numeric value and a...
Read more >
Method for Time Slicing in Netcdf similar to xarray
The easiest approach I could find is from netCDF4 import date2index from datetime import datetime timeindex = date2index(datetime(1990,12,1) ...
Read more >
2 Components of a NetCDF Dataset
NetCDF dimension declarations appear after the dimensions keyword, netCDF variables and attributes are defined after the variables keyword, and variable data ...
Read more >
Database Engine events and errors - SQL Server
139, 15, No, Cannot assign a default value to a local variable. ... 566, 21, Yes, An error occurred while writing an audit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found