45
45
from datetime import datetime , timedelta
46
46
from enum import Enum
47
47
from functools import partial
48
- from typing import ClassVar
48
+ from typing import TYPE_CHECKING , ClassVar , Literal
49
49
50
50
import numpy as np
51
51
import pandas as pd
67
67
cftime = None
68
68
69
69
70
+ if TYPE_CHECKING :
71
+ from xarray .core .types import InclusiveOptions , SideOptions
72
+
73
+
70
74
def get_date_type (calendar , use_cftime = True ):
71
75
"""Return the cftime date type for a given calendar name."""
72
76
if cftime is None :
@@ -852,15 +856,24 @@ def _generate_range(start, end, periods, offset):
852
856
853
857
854
858
class _NoDefault (Enum ):
855
- """Used by pandas to specify a default value for a deprecated argument."""
859
+ """Used by pandas to specify a default value for a deprecated argument.
860
+ Copied from pandas._libs.lib._NoDefault.
861
+ """
856
862
857
863
no_default = "NO_DEFAULT"
858
864
859
865
def __repr__ (self ) -> str :
860
866
return "<no_default>"
861
867
862
868
869
+ no_default = (
870
+ _NoDefault .no_default
871
+ ) # Sentinel indicating the default value following pandas
872
+ NoDefault = Literal [_NoDefault .no_default ] # For typing following pandas
873
+
874
+
863
875
def _translate_closed_to_inclusive (closed ):
876
+ """Follows code added in pandas #43504."""
864
877
emit_user_level_warning (
865
878
"Following pandas, the `closed` argument is deprecated in "
866
879
"favor of the `inclusive` argument, and will be removed in "
@@ -880,12 +893,13 @@ def _translate_closed_to_inclusive(closed):
880
893
881
894
882
895
def _infer_inclusive (closed , inclusive ):
883
- if closed is not _NoDefault and inclusive is not None :
896
+ """Follows code added in pandas #43504."""
897
+ if closed is not no_default and inclusive is not None :
884
898
raise ValueError (
885
- "Deprecated argument `closed` cannot be passed if "
886
- "argument `inclusive` is not None."
899
+ "Following pandas, deprecated argument `closed` cannot be "
900
+ "passed if argument `inclusive` is not None."
887
901
)
888
- if closed is not _NoDefault :
902
+ if closed is not no_default :
889
903
inclusive = _translate_closed_to_inclusive (closed )
890
904
elif inclusive is None :
891
905
inclusive = "both"
@@ -899,8 +913,8 @@ def cftime_range(
899
913
freq = "D" ,
900
914
normalize = False ,
901
915
name = None ,
902
- closed = _NoDefault ,
903
- inclusive = None ,
916
+ closed : NoDefault | SideOptions = no_default ,
917
+ inclusive : None | InclusiveOptions = None ,
904
918
calendar = "standard" ,
905
919
):
906
920
"""Return a fixed frequency CFTimeIndex.
@@ -922,8 +936,15 @@ def cftime_range(
922
936
closed : {"left", "right"} or None, default: _NoDefault
923
937
Make the interval closed with respect to the given frequency to the
924
938
"left", "right", or both sides (None).
939
+
940
+ .. deprecated:: 2023.01.1
941
+ Following pandas, the `closed` argument is deprecated in favor
942
+ of the `inclusive` argument, and will be removed in a future
943
+ version of xarray.
925
944
inclusive : {None, "both", "neither", "left", "right"}, default None
926
- Include boundaries; Whether to set each bound as closed or open.
945
+ Include boundaries; whether to set each bound as closed or open.
946
+
947
+ .. versionadded:: 2023.01.1
927
948
calendar : str, default: "standard"
928
949
Calendar type for the datetimes.
929
950
@@ -938,7 +959,6 @@ def cftime_range(
938
959
features of ``pandas.date_range`` (e.g. specifying how the index is
939
960
``closed`` on either side, or whether or not to ``normalize`` the start and
940
961
end bounds); however, there are some notable exceptions:
941
-
942
962
- You cannot specify a ``tz`` (time zone) argument.
943
963
- Start or end dates specified as partial-datetime strings must use the
944
964
`ISO-8601 format <https://en.wikipedia.org/wiki/ISO_8601>`_.
@@ -1129,8 +1149,8 @@ def date_range(
1129
1149
tz = None ,
1130
1150
normalize = False ,
1131
1151
name = None ,
1132
- closed = _NoDefault ,
1133
- inclusive = None ,
1152
+ closed : NoDefault | SideOptions = no_default ,
1153
+ inclusive : None | InclusiveOptions = None ,
1134
1154
calendar = "standard" ,
1135
1155
use_cftime = None ,
1136
1156
):
@@ -1160,8 +1180,15 @@ def date_range(
1160
1180
closed : {"left", "right"} or None, default: _NoDefault
1161
1181
Make the interval closed with respect to the given frequency to the
1162
1182
"left", "right", or both sides (None).
1183
+
1184
+ .. deprecated:: 2023.01.1
1185
+ Following pandas, the `closed` argument is deprecated in favor
1186
+ of the `inclusive` argument, and will be removed in a future
1187
+ version of xarray.
1163
1188
inclusive : {None, "both", "neither", "left", "right"}, default None
1164
- Include boundaries; Whether to set each bound as closed or open.
1189
+ Include boundaries; whether to set each bound as closed or open.
1190
+
1191
+ .. versionadded:: 2023.01.1
1165
1192
calendar : str, default: "standard"
1166
1193
Calendar type for the datetimes.
1167
1194
use_cftime : boolean, optional
0 commit comments