pydata / pydata/xarray

xr.DataArray.to_series returns a (mutable) view

Open
#2,949 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug contrib-help-wanted
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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__'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The entry points shown are DataArray.from_series, DataArray.to_series, and get_index; start by tracing how to_series constructs the pandas Series and its index. Add a regression check that changing the returned Series index.name does not change the DataArray coordinate name, and verify the provided example still reports forward_date on every to_series() call.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.