Skip to content

Commit 69dc2a0

Browse files
max-sixtyIllviljan
andauthored
Change concat dims to be Hashable (#6121)
* Change concat dims to be Hashable * Add Literal types & tests * Update xarray/tests/test_concat.py Co-authored-by: Illviljan <[email protected]> * Update xarray/core/concat.py Co-authored-by: Illviljan <[email protected]> * Update xarray/core/concat.py Co-authored-by: Illviljan <[email protected]> * add annotations Co-authored-by: Illviljan <[email protected]>
1 parent 5b322c9 commit 69dc2a0

File tree

2 files changed

+71
-49
lines changed

2 files changed

+71
-49
lines changed

xarray/core/concat.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import (
24
TYPE_CHECKING,
35
Dict,
@@ -12,6 +14,7 @@
1214
)
1315

1416
import pandas as pd
17+
from typing_extensions import Literal
1518

1619
from . import dtypes, utils
1720
from .alignment import align
@@ -24,14 +27,19 @@
2427
from .dataarray import DataArray
2528
from .dataset import Dataset
2629

30+
compat_options = Literal[
31+
"identical", "equals", "broadcast_equals", "no_conflicts", "override"
32+
]
33+
concat_options = Literal["all", "minimal", "different"]
34+
2735

2836
@overload
2937
def concat(
3038
objs: Iterable["Dataset"],
31-
dim: Union[str, "DataArray", pd.Index],
32-
data_vars: Union[str, List[str]] = "all",
33-
coords: Union[str, List[str]] = "different",
34-
compat: str = "equals",
39+
dim: Hashable | "DataArray" | pd.Index,
40+
data_vars: concat_options | List[Hashable] = "all",
41+
coords: concat_options | List[Hashable] = "different",
42+
compat: compat_options = "equals",
3543
positions: Optional[Iterable[int]] = None,
3644
fill_value: object = dtypes.NA,
3745
join: str = "outer",
@@ -43,10 +51,10 @@ def concat(
4351
@overload
4452
def concat(
4553
objs: Iterable["DataArray"],
46-
dim: Union[str, "DataArray", pd.Index],
47-
data_vars: Union[str, List[str]] = "all",
48-
coords: Union[str, List[str]] = "different",
49-
compat: str = "equals",
54+
dim: Hashable | "DataArray" | pd.Index,
55+
data_vars: concat_options | List[Hashable] = "all",
56+
coords: concat_options | List[Hashable] = "different",
57+
compat: compat_options = "equals",
5058
positions: Optional[Iterable[int]] = None,
5159
fill_value: object = dtypes.NA,
5260
join: str = "outer",
@@ -74,14 +82,14 @@ def concat(
7482
xarray objects to concatenate together. Each object is expected to
7583
consist of variables and coordinates with matching shapes except for
7684
along the concatenated dimension.
77-
dim : str or DataArray or pandas.Index
85+
dim : Hashable or DataArray or pandas.Index
7886
Name of the dimension to concatenate along. This can either be a new
7987
dimension name, in which case it is added along axis=0, or an existing
8088
dimension name, in which case the location of the dimension is
8189
unchanged. If dimension is provided as a DataArray or Index, its name
8290
is used as the dimension to concatenate along and the values are added
8391
as a coordinate.
84-
data_vars : {"minimal", "different", "all"} or list of str, optional
92+
data_vars : {"minimal", "different", "all"} or list of Hashable, optional
8593
These data variables will be concatenated together:
8694
* "minimal": Only data variables in which the dimension already
8795
appears are included.
@@ -91,11 +99,11 @@ def concat(
9199
load the data payload of data variables into memory if they are not
92100
already loaded.
93101
* "all": All data variables will be concatenated.
94-
* list of str: The listed data variables will be concatenated, in
102+
* list of dims: The listed data variables will be concatenated, in
95103
addition to the "minimal" data variables.
96104
97105
If objects are DataArrays, data_vars must be "all".
98-
coords : {"minimal", "different", "all"} or list of str, optional
106+
coords : {"minimal", "different", "all"} or list of Hashable, optional
99107
These coordinate variables will be concatenated together:
100108
* "minimal": Only coordinates in which the dimension already appears
101109
are included.
@@ -106,7 +114,7 @@ def concat(
106114
loaded.
107115
* "all": All coordinate variables will be concatenated, except
108116
those corresponding to other dimensions.
109-
* list of str: The listed coordinate variables will be concatenated,
117+
* list of Hashable: The listed coordinate variables will be concatenated,
110118
in addition to the "minimal" coordinates.
111119
compat : {"identical", "equals", "broadcast_equals", "no_conflicts", "override"}, optional
112120
String indicating how to compare non-concatenated variables of the same name for

0 commit comments

Comments
 (0)