# XARRAY: ValueError: cannot reindex or align along dimension ‘x’ because the index has duplicate values

**URL:** <https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423>\
**Category:** Data\
**Created:** [May 7, 2023, 5:49pm UTC](https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423 "2023-05-07T17:49:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LauraHornik](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LauraHornik](https://discourse.pangeo.io/u/LauraHornik)\
**Post date:** [May 7, 2023, 5:49pm UTC](https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423/1 "2023-05-07T17:49:58Z")

</div>

I have around 5000 geotiff files each of (25000,25000) with data value float32.  
I wanted to calculate .argmax() across 5000 arrays.  
So, first tried by merging the data arrays.

import xarray, rioxarray  
rasters = [rioxarray.open\_rasterio(f) for f in files]  
merged = xarray.concat(rasters,dim=“band”)

I m struggled with the following error.

Traceback (most recent call last):  
File “”, line 42, in  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/concat.py”, line 192, in concat  
objs, dim, data\_vars, coords, compat, positions, fill\_value, join, combine\_attrs  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/concat.py”, line 527, in \_dataarray\_concat  
combine\_attrs=“drop”,  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/concat.py”, line 384, in \_dataset\_concat  
align(\*datasets, join=join, copy=False, exclude=[dim], fill\_value=fill\_value)  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/alignment.py”, line 354, in align  
copy=copy, fill\_value=fill\_value, indexers=valid\_indexers  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/dataset.py”, line 2630, in reindex  
\*\*indexers\_kwargs,  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/dataset.py”, line 2661, in \_reindex  
sparse=sparse,  
File “/home/laura/.local/lib/python3.6/site-packages/xarray/core/alignment.py”, line 567, in reindex\_variables  
“index has duplicate values” % dim  
ValueError: cannot reindex or align along dimension ‘x’ because the index has duplicate values

I tried by putting compat = override or no\_conflicts. But, I got same error.

Could someone help me ?

---

<div class="post-metadata">

**Author:** ![TomAugspurger](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/tomaugspurger/32/21_2.png) [@TomAugspurger](https://discourse.pangeo.io/u/TomAugspurger)\
**Post date:** [May 7, 2023, 7:29pm UTC](https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423/2 "2023-05-07T19:29:50Z")

</div>

What kind of joining / alignment behavior do you expect here? Are all the rasters supposed to have the exact same footprint, and if not, how do you want to align things? By doing a `concat`, you’re telling xarray you want a single array, and it has to figure out how to do things (maybe you want `join="override"`?)

It’s also worth asking if you even need to align things in the first place. Is doing each `argmax` independently an option, so that you end up with `5,000` argmaxes? And then you can take the `argmax` of that array of `argmaxes` to get the overall argmax? If you get the alignment figured out and are using Dask to parallelize things, that’s essentially what it would do for you.

---

<div class="post-metadata">

**Author:** ![LauraHornik](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LauraHornik](https://discourse.pangeo.io/u/LauraHornik)\
**Post date:** [May 8, 2023, 1:29pm UTC](https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423/3 "2023-05-08T13:29:53Z")

</div>

Thanks for your kind response. I tried with join=“override” but got the same error.  
I also added compat=no\_conflicts, but still the same error.  
All the rasters have same footprint, and used same `x` and `y` dimensions.  
It was supposed to align over `x` and `y` dimensions and the output into the following shape (5000, 25000, 25000) where `5000` is no of bands. So I will calculate argmax across axis 0.  
All input files were read as (1, 25000, 25000) using rioxarray.open\_rasterio().

But when using concat, got the error. As all of them are read as same shape of (1, 25000, 25000), it is not necessary to align, just needed to concat.

---

<div class="post-metadata">

**Author:** ![TomAugspurger](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/tomaugspurger/32/21_2.png) [@TomAugspurger](https://discourse.pangeo.io/u/TomAugspurger)\
**Post date:** [May 8, 2023, 3:02pm UTC](https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423/4 "2023-05-08T15:02:32Z")

</div>

Are you able to share one or two of the files?

---

<div class="post-metadata">

**Author:** ![LauraHornik](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@LauraHornik](https://discourse.pangeo.io/u/LauraHornik)\
**Post date:** [May 8, 2023, 3:50pm UTC](https://discourse.pangeo.io/t/xarray-valueerror-cannot-reindex-or-align-along-dimension-x-because-the-index-has-duplicate-values/3423/5 "2023-05-08T15:50:52Z")

</div>

I will try to isolate problematic file and share.
