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.

xr.DataArray.to_series returns a (mutable) view

See original GitHub issue

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd
import xarray as xr

dates = pd.date_range('01-Jan-2019', '01-Jan-2020', name='forward_date')[:-1]
s = pd.Series(np.random.randn(dates.size), dates)
array = xr.DataArray.from_series(s)
In [52]: array
Out[52]: 
<xarray.DataArray (forward_date: 365)>
array([ 0.511149, -1.009054, -0.098397, ...,  0.496596,  1.435346,  0.21206 ])
Coordinates:
  * forward_date  (forward_date) datetime64[ns] 2019-01-01 ... 2019-12-31

In [53]: array.to_series().index.name
Out[53]: 'forward_date'

In [54]: array.to_series().index.name = 'peristent!!!'

In [55]: array.to_series().index.name
Out[55]: 'peristent!!!'

In [56]: array.get_index('forward_date')
Out[56]: 
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
               '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
               '2019-01-09', '2019-01-10',
               ...
               '2019-12-22', '2019-12-23', '2019-12-24', '2019-12-25',
               '2019-12-26', '2019-12-27', '2019-12-28', '2019-12-29',
               '2019-12-30', '2019-12-31'],
              dtype='datetime64[ns]', name='peristent!!!', length=365, freq='D')

Problem description

IMHO Setting index.name on the returned series shouldn’t affect the original DataArray - i.e. every call to array.to_series().index.name should return forward_date irrespective of any mutating operations performed on the returned series.

Expected Output

Output of xr.show_versions()

In [48]: xr.show_versions()
Traceback (most recent call last):

  File "<ipython-input-48-6f391305f2fe>", line 1, in <module>
    xr.show_versions()

  File "C:\Users\dhirschf\envs\dev\lib\site-packages\xarray\util\print_versions.py", line 84, in show_versions
    sys_info.extend(netcdf_and_hdf5_versions())

  File "C:\Users\dhirschf\envs\dev\lib\site-packages\xarray\util\print_versions.py", line 75, in netcdf_and_hdf5_versions
    libhdf5_version = h5py.__hdf5libversion__

AttributeError: module 'h5py' has no attribute '__hdf5libversion__'

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gwgundersencommented, Jul 14, 2019

I’ve created PR https://github.com/pydata/xarray/pull/3126 to fix this issue.

0reactions
sjvrijncommented, Nov 16, 2020

Since PR #3126 seems to be closed due to performance issues, is this underlying issue still considered an issue, or should it be closed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's New - Xarray
Added a new Dataset.chunksizes , DataArray.chunksizes , and Variable.chunksizes property, which will always return a mapping from dimension names to ...
Read more >
What's New — xarray 0.10.2 documentation
DataArrayRolling() object now supports construct() method that returns a view of the DataArray / Dataset object with the rolling-window dimension added to ...
Read more >
xarray.DataArray.to_series
Convert this array into a pandas.Series. The Series is indexed by the Cartesian product of index coordinates (in the form of a pandas.MultiIndex...
Read more >
xarray.DataArray
A view of the array's data is used instead of a copy if possible. ... Returns a new DataArray with duplicate dimension values...
Read more >
Indexing and selecting data - Xarray
We can also use these methods to index all variables in a dataset simultaneously, returning a new dataset: In [21]: da = xr.DataArray(...
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