4545from datetime import datetime , timedelta
4646from enum import Enum
4747from functools import partial
48- from typing import ClassVar
48+ from typing import TYPE_CHECKING , ClassVar , Literal
4949
5050import numpy as np
5151import pandas as pd
6767 cftime = None
6868
6969
70+ if TYPE_CHECKING :
71+ from xarray .core .types import InclusiveOptions , SideOptions
72+
73+
7074def get_date_type (calendar , use_cftime = True ):
7175 """Return the cftime date type for a given calendar name."""
7276 if cftime is None :
@@ -852,15 +856,24 @@ def _generate_range(start, end, periods, offset):
852856
853857
854858class _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+ """
856862
857863 no_default = "NO_DEFAULT"
858864
859865 def __repr__ (self ) -> str :
860866 return "<no_default>"
861867
862868
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+
863875def _translate_closed_to_inclusive (closed ):
876+ """Follows code added in pandas #43504."""
864877 emit_user_level_warning (
865878 "Following pandas, the `closed` argument is deprecated in "
866879 "favor of the `inclusive` argument, and will be removed in "
@@ -880,12 +893,13 @@ def _translate_closed_to_inclusive(closed):
880893
881894
882895def _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 :
884898 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."
887901 )
888- if closed is not _NoDefault :
902+ if closed is not no_default :
889903 inclusive = _translate_closed_to_inclusive (closed )
890904 elif inclusive is None :
891905 inclusive = "both"
@@ -899,8 +913,8 @@ def cftime_range(
899913 freq = "D" ,
900914 normalize = False ,
901915 name = None ,
902- closed = _NoDefault ,
903- inclusive = None ,
916+ closed : NoDefault | SideOptions = no_default ,
917+ inclusive : None | InclusiveOptions = None ,
904918 calendar = "standard" ,
905919):
906920 """Return a fixed frequency CFTimeIndex.
@@ -922,8 +936,15 @@ def cftime_range(
922936 closed : {"left", "right"} or None, default: _NoDefault
923937 Make the interval closed with respect to the given frequency to the
924938 "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.
925944 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
927948 calendar : str, default: "standard"
928949 Calendar type for the datetimes.
929950
@@ -938,7 +959,6 @@ def cftime_range(
938959 features of ``pandas.date_range`` (e.g. specifying how the index is
939960 ``closed`` on either side, or whether or not to ``normalize`` the start and
940961 end bounds); however, there are some notable exceptions:
941-
942962 - You cannot specify a ``tz`` (time zone) argument.
943963 - Start or end dates specified as partial-datetime strings must use the
944964 `ISO-8601 format <https://en.wikipedia.org/wiki/ISO_8601>`_.
@@ -1129,8 +1149,8 @@ def date_range(
11291149 tz = None ,
11301150 normalize = False ,
11311151 name = None ,
1132- closed = _NoDefault ,
1133- inclusive = None ,
1152+ closed : NoDefault | SideOptions = no_default ,
1153+ inclusive : None | InclusiveOptions = None ,
11341154 calendar = "standard" ,
11351155 use_cftime = None ,
11361156):
@@ -1160,8 +1180,15 @@ def date_range(
11601180 closed : {"left", "right"} or None, default: _NoDefault
11611181 Make the interval closed with respect to the given frequency to the
11621182 "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.
11631188 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
11651192 calendar : str, default: "standard"
11661193 Calendar type for the datetimes.
11671194 use_cftime : boolean, optional
0 commit comments