home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 1084277555

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/6377#issuecomment-1084277555 https://api.github.com/repos/pydata/xarray/issues/6377 1084277555 IC_kwDOAMm_X85AoMMz 13662783 2022-03-31T08:45:18Z 2022-03-31T08:45:18Z CONTRIBUTOR

@Jeitan

The coordinate is a DataArray as well, so the following would work:

```python

Example DataArray

da = xr.DataArray(np.ones((3, 3)), {"y": [50.0, 60.0, 70.0], "x": [1.0, 2.0, 3.0]}, ("y", "x"))

Replace 50.0 and 60.0 by 5.0 and 6.0 in the y coordinate

da["y"] = da["y"].replace_values([50.0, 60.0], [5.0, 6.0]) ```

Your example in the other issue mentions one of the ways you'd replace in pandas, but for a dataframe. With a dataframe, there's quite some flexibility:

python df.replace({0: 10, 1: 100}) df.replace({'A': 0, 'B': 5}, 100) df.replace({'A': {0: 100, 4: 400}})

I'd say the xarray counterpart of a Dataframe is a Dataset; the counterpart of a DataArray is a Series. Replacing the coordinates in a DataArray is akin to replacing the values of the index of a Series, which is apparently possible with series.rename(index={from: to}).

Other thoughts: some complexity comes in when implementing a replace_values method for a Dataset. I also think the pandas replace method signature is too complicated (scalars, lists, dicts, dicts of dicts, probably more?) and the docstring is quite extensive (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html)

I think the question is what the signature should be. You could compare to reindex (https://xarray.pydata.org/en/stable/generated/xarray.Dataset.reindex.html) and have an "replacer" argument:

```python da = da.replace({"y": ([50.0, 60.0], [5.0, 6.0])})

da["y"] = da["y"].replace([50.0, 60.0], [5.0, 6.0]) ```

The first one would also work for Datasets, but I personally prefer the second one for it's simplicity (and which is maybe closer to .where : https://xarray.pydata.org/en/stable/generated/xarray.DataArray.where.html).

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