1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
|
"""Time offset classes for use with cftime.datetime objects"""
# The offset classes and mechanisms for generating time ranges defined in
# this module were copied/adapted from those defined in pandas. See in
# particular the objects and methods defined in pandas.tseries.offsets
# and pandas.core.indexes.datetimes.
# For reference, here is a copy of the pandas copyright notice:
# (c) 2011-2012, Lambda Foundry, Inc. and PyData Development Team
# All rights reserved.
# Copyright (c) 2008-2011 AQR Capital Management, LLC
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the copyright holder nor the names of any
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import annotations
import re
import warnings
from collections.abc import Mapping
from datetime import datetime, timedelta
from functools import partial
from typing import TYPE_CHECKING, ClassVar, Literal, TypeVar, get_args
import numpy as np
import pandas as pd
from packaging.version import Version
from xarray.coding.cftimeindex import CFTimeIndex
from xarray.coding.times import (
_is_standard_calendar,
_parse_iso8601,
_should_cftime_be_used,
convert_time_or_go_back,
format_cftime_datetime,
)
from xarray.compat.pdcompat import (
count_not_none,
default_precision_timestamp,
)
from xarray.core.common import _contains_datetime_like_objects, is_np_datetime_like
from xarray.core.types import InclusiveOptions
from xarray.core.utils import attempt_import, emit_user_level_warning
if TYPE_CHECKING:
from xarray.core.types import (
PDDatetimeUnitOptions,
Self,
TypeAlias,
)
DayOption: TypeAlias = Literal["start", "end"]
T_FreqStr = TypeVar("T_FreqStr", str, None)
def get_date_type(calendar, use_cftime=True):
"""Return the cftime date type for a given calendar name."""
if TYPE_CHECKING:
import cftime
else:
cftime = attempt_import("cftime")
if _is_standard_calendar(calendar) and not use_cftime:
return default_precision_timestamp
calendars = {
"noleap": cftime.DatetimeNoLeap,
"360_day": cftime.Datetime360Day,
"365_day": cftime.DatetimeNoLeap,
"366_day": cftime.DatetimeAllLeap,
"gregorian": cftime.DatetimeGregorian,
"proleptic_gregorian": cftime.DatetimeProlepticGregorian,
"julian": cftime.DatetimeJulian,
"all_leap": cftime.DatetimeAllLeap,
"standard": cftime.DatetimeGregorian,
}
return calendars[calendar]
class BaseCFTimeOffset:
_freq: ClassVar[str | None] = None
_day_option: ClassVar[DayOption | None] = None
n: int
def __init__(self, n: int = 1) -> None:
if not isinstance(n, int):
raise TypeError(
"The provided multiple 'n' must be an integer. "
f"Instead a value of type {type(n)!r} was provided."
)
self.n = n
def rule_code(self) -> str | None:
return self._freq
def __eq__(self, other: object) -> bool:
if not isinstance(other, BaseCFTimeOffset):
return NotImplemented
return self.n == other.n and self.rule_code() == other.rule_code()
def __ne__(self, other: object) -> bool:
return not self == other
def __add__(self, other):
return self.__apply__(other)
def __sub__(self, other):
if TYPE_CHECKING:
import cftime
else:
cftime = attempt_import("cftime")
if isinstance(other, cftime.datetime):
raise TypeError("Cannot subtract a cftime.datetime from a time offset.")
elif type(other) is type(self):
return type(self)(self.n - other.n)
else:
return NotImplemented
def __mul__(self, other: int) -> Self:
if not isinstance(other, int):
return NotImplemented
return type(self)(n=other * self.n)
def __neg__(self) -> Self:
return self * -1
def __rmul__(self, other):
return self.__mul__(other)
def __radd__(self, other):
return self.__add__(other)
def __rsub__(self, other):
if isinstance(other, BaseCFTimeOffset) and type(self) is not type(other):
raise TypeError("Cannot subtract cftime offsets of differing types")
return -self + other
def __apply__(self, other):
return NotImplemented
def onOffset(self, date) -> bool:
"""Check if the given date is in the set of possible dates created
using a length-one version of this offset class."""
test_date = (self + date) - self
return date == test_date
def rollforward(self, date):
if self.onOffset(date):
return date
else:
return date + type(self)()
def rollback(self, date):
if self.onOffset(date):
return date
else:
return date - type(self)()
def __str__(self):
return f"<{type(self).__name__}: n={self.n}>"
def __repr__(self):
return str(self)
def _get_offset_day(self, other):
# subclass must implement `_day_option`; calling from the base class
# will raise NotImplementedError.
return _get_day_of_month(other, self._day_option)
class Tick(BaseCFTimeOffset):
# analogous https://github.com/pandas-dev/pandas/blob/ccb25ab1d24c4fb9691270706a59c8d319750870/pandas/_libs/tslibs/offsets.pyx#L806
def _next_higher_resolution(self) -> Tick:
self_type = type(self)
if self_type is Day:
return Hour(self.n * 24)
if self_type is Hour:
return Minute(self.n * 60)
if self_type is Minute:
return Second(self.n * 60)
if self_type is Second:
return Millisecond(self.n * 1000)
if self_type is Millisecond:
return Microsecond(self.n * 1000)
raise ValueError("Could not convert to integer offset at any resolution")
def __mul__(self, other: int | float) -> Tick:
if not isinstance(other, int | float):
return NotImplemented
if isinstance(other, float):
n = other * self.n
# If the new `n` is an integer, we can represent it using the
# same BaseCFTimeOffset subclass as self, otherwise we need to move up
# to a higher-resolution subclass
if np.isclose(n % 1, 0):
return type(self)(int(n))
new_self = self._next_higher_resolution()
return new_self * other
return type(self)(n=other * self.n)
def as_timedelta(self) -> timedelta:
"""All Tick subclasses must implement an as_timedelta method."""
raise NotImplementedError
def _get_day_of_month(other, day_option: DayOption) -> int:
"""Find the day in `other`'s month that satisfies a BaseCFTimeOffset's
onOffset policy, as described by the `day_option` argument.
Parameters
----------
other : cftime.datetime
day_option : 'start', 'end'
'start': returns 1
'end': returns last day of the month
Returns
-------
day_of_month : int
"""
if day_option == "start":
return 1
elif day_option == "end":
return other.daysinmonth
elif day_option is None:
# Note: unlike `_shift_month`, _get_day_of_month does not
# allow day_option = None
raise NotImplementedError()
raise ValueError(day_option)
def _adjust_n_months(other_day, n, reference_day):
"""Adjust the number of times a monthly offset is applied based
on the day of a given date, and the reference day provided.
"""
if n > 0 and other_day < reference_day:
n = n - 1
elif n <= 0 and other_day > reference_day:
n = n + 1
return n
def _adjust_n_years(other, n, month, reference_day):
"""Adjust the number of times an annual offset is applied based on
another date, and the reference day provided"""
if n > 0:
if other.month < month or (other.month == month and other.day < reference_day):
n -= 1
elif other.month > month or (other.month == month and other.day > reference_day):
n += 1
return n
def _shift_month(date, months, day_option: DayOption = "start"):
"""Shift the date to a month start or end a given number of months away."""
_ = attempt_import("cftime")
has_year_zero = date.has_year_zero
year = date.year + (date.month + months) // 12
month = (date.month + months) % 12
if month == 0:
month = 12
year -= 1
if not has_year_zero:
if date.year < 0 <= year:
year += 1
elif year <= 0 < date.year:
year -= 1
# Silence warnings associated with generating dates with years < 1.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="this date/calendar/year zero")
if day_option == "start":
day = 1
elif day_option == "end":
reference = type(date)(year, month, 1, has_year_zero=has_year_zero)
day = reference.daysinmonth
else:
raise ValueError(day_option)
return date.replace(year=year, month=month, day=day)
def roll_qtrday(
other, n: int, month: int, day_option: DayOption, modby: int = 3
) -> int:
"""Possibly increment or decrement the number of periods to shift
based on rollforward/rollbackward conventions.
Parameters
----------
other : cftime.datetime
n : number of periods to increment, before adjusting for rolling
month : int reference month giving the first month of the year
day_option : 'start', 'end'
The convention to use in finding the day in a given month against
which to compare for rollforward/rollbackward decisions.
modby : int 3 for quarters, 12 for years
Returns
-------
n : int number of periods to increment
See Also
--------
_get_day_of_month : Find the day in a month provided an offset.
"""
months_since = other.month % modby - month % modby
if n > 0:
if months_since < 0 or (
months_since == 0 and other.day < _get_day_of_month(other, day_option)
):
# pretend to roll back if on same month but
# before compare_day
n -= 1
elif months_since > 0 or (
months_since == 0 and other.day > _get_day_of_month(other, day_option)
):
# make sure to roll forward, so negate
n += 1
return n
def _validate_month(month: int | None, default_month: int) -> int:
result_month = default_month if month is None else month
if not isinstance(result_month, int):
raise TypeError(
"'self.month' must be an integer value between 1 "
"and 12. Instead, it was set to a value of "
f"{result_month!r}"
)
elif not (1 <= result_month <= 12):
raise ValueError(
"'self.month' must be an integer value between 1 "
"and 12. Instead, it was set to a value of "
f"{result_month!r}"
)
return result_month
class MonthBegin(BaseCFTimeOffset):
_freq = "MS"
def __apply__(self, other):
n = _adjust_n_months(other.day, self.n, 1)
return _shift_month(other, n, "start")
def onOffset(self, date) -> bool:
"""Check if the given date is in the set of possible dates created
using a length-one version of this offset class."""
return date.day == 1
class MonthEnd(BaseCFTimeOffset):
_freq = "ME"
def __apply__(self, other):
n = _adjust_n_months(other.day, self.n, other.daysinmonth)
return _shift_month(other, n, "end")
def onOffset(self, date) -> bool:
"""Check if the given date is in the set of possible dates created
using a length-one version of this offset class."""
return date.day == date.daysinmonth
_MONTH_ABBREVIATIONS = {
1: "JAN",
2: "FEB",
3: "MAR",
4: "APR",
5: "MAY",
6: "JUN",
7: "JUL",
8: "AUG",
9: "SEP",
10: "OCT",
11: "NOV",
12: "DEC",
}
class QuarterOffset(BaseCFTimeOffset):
"""Quarter representation copied off of pandas/tseries/offsets.py"""
_default_month: ClassVar[int]
month: int
def __init__(self, n: int = 1, month: int | None = None) -> None:
BaseCFTimeOffset.__init__(self, n)
self.month = _validate_month(month, self._default_month)
def __apply__(self, other):
# months_since: find the calendar quarter containing other.month,
# e.g. if other.month == 8, the calendar quarter is [Jul, Aug, Sep].
# Then find the month in that quarter containing an onOffset date for
# self. `months_since` is the number of months to shift other.month
# to get to this on-offset month.
months_since = other.month % 3 - self.month % 3
qtrs = roll_qtrday(
other, self.n, self.month, day_option=self._day_option, modby=3
)
months = qtrs * 3 - months_since
return _shift_month(other, months, self._day_option)
def onOffset(self, date) -> bool:
"""Check if the given date is in the set of possible dates created
using a length-one version of this offset class."""
mod_month = (date.month - self.month) % 3
return mod_month == 0 and date.day == self._get_offset_day(date)
def __sub__(self, other: Self) -> Self:
if TYPE_CHECKING:
import cftime
else:
cftime = attempt_import("cftime")
if isinstance(other, cftime.datetime):
raise TypeError("Cannot subtract cftime.datetime from offset.")
if type(other) is type(self) and other.month == self.month:
return type(self)(self.n - other.n, month=self.month)
return NotImplemented
def __mul__(self, other):
if isinstance(other, float):
return NotImplemented
return type(self)(n=other * self.n, month=self.month)
def rule_code(self) -> str:
return f"{self._freq}-{_MONTH_ABBREVIATIONS[self.month]}"
def __str__(self):
return f"<{type(self).__name__}: n={self.n}, month={self.month}>"
class QuarterBegin(QuarterOffset):
# When converting a string to an offset, pandas converts
# 'QS' to a QuarterBegin offset starting in the month of
# January. When creating a QuarterBegin offset directly
# from the constructor, however, the default month is March.
# We follow that behavior here.
_default_month = 3
_freq = "QS"
_day_option = "start"
def rollforward(self, date):
"""Roll date forward to nearest start of quarter"""
if self.onOffset(date):
return date
else:
return date + QuarterBegin(month=self.month)
def rollback(self, date):
"""Roll date backward to nearest start of quarter"""
if self.onOffset(date):
return date
else:
return date - QuarterBegin(month=self.month)
class QuarterEnd(QuarterOffset):
# When converting a string to an offset, pandas converts
# 'Q' to a QuarterEnd offset starting in the month of
# December. When creating a QuarterEnd offset directly
# from the constructor, however, the default month is March.
# We follow that behavior here.
_default_month = 3
_freq = "QE"
_day_option = "end"
def rollforward(self, date):
"""Roll date forward to nearest end of quarter"""
if self.onOffset(date):
return date
else:
return date + QuarterEnd(month=self.month)
def rollback(self, date):
"""Roll date backward to nearest end of quarter"""
if self.onOffset(date):
return date
else:
return date - QuarterEnd(month=self.month)
class YearOffset(BaseCFTimeOffset):
_default_month: ClassVar[int]
month: int
def __init__(self, n: int = 1, month: int | None = None) -> None:
BaseCFTimeOffset.__init__(self, n)
self.month = _validate_month(month, self._default_month)
def __apply__(self, other):
reference_day = _get_day_of_month(other, self._day_option)
years = _adjust_n_years(other, self.n, self.month, reference_day)
months = years * 12 + (self.month - other.month)
return _shift_month(other, months, self._day_option)
def __sub__(self, other):
if TYPE_CHECKING:
import cftime
else:
cftime = attempt_import("cftime")
if isinstance(other, cftime.datetime):
raise TypeError("Cannot subtract cftime.datetime from offset.")
elif type(other) is type(self) and other.month == self.month:
return type(self)(self.n - other.n, month=self.month)
else:
return NotImplemented
def __mul__(self, other):
if isinstance(other, float):
return NotImplemented
return type(self)(n=other * self.n, month=self.month)
def rule_code(self) -> str:
return f"{self._freq}-{_MONTH_ABBREVIATIONS[self.month]}"
def __str__(self) -> str:
return f"<{type(self).__name__}: n={self.n}, month={self.month}>"
class YearBegin(YearOffset):
_freq = "YS"
_day_option = "start"
_default_month = 1
def onOffset(self, date) -> bool:
"""Check if the given date is in the set of possible dates created
using a length-one version of this offset class."""
return date.day == 1 and date.month == self.month
def rollforward(self, date):
"""Roll date forward to nearest start of year"""
if self.onOffset(date):
return date
else:
return date + YearBegin(month=self.month)
def rollback(self, date):
"""Roll date backward to nearest start of year"""
if self.onOffset(date):
return date
else:
return date - YearBegin(month=self.month)
class YearEnd(YearOffset):
_freq = "YE"
_day_option = "end"
_default_month = 12
def onOffset(self, date) -> bool:
"""Check if the given date is in the set of possible dates created
using a length-one version of this offset class."""
return date.day == date.daysinmonth and date.month == self.month
def rollforward(self, date):
"""Roll date forward to nearest end of year"""
if self.onOffset(date):
return date
else:
return date + YearEnd(month=self.month)
def rollback(self, date):
"""Roll date backward to nearest end of year"""
if self.onOffset(date):
return date
else:
return date - YearEnd(month=self.month)
class Day(Tick):
_freq = "D"
def as_timedelta(self) -> timedelta:
return timedelta(days=self.n)
def __apply__(self, other):
return other + self.as_timedelta()
class Hour(Tick):
_freq = "h"
def as_timedelta(self) -> timedelta:
return timedelta(hours=self.n)
def __apply__(self, other):
return other + self.as_timedelta()
class Minute(Tick):
_freq = "min"
def as_timedelta(self) -> timedelta:
return timedelta(minutes=self.n)
def __apply__(self, other):
return other + self.as_timedelta()
class Second(Tick):
_freq = "s"
def as_timedelta(self) -> timedelta:
return timedelta(seconds=self.n)
def __apply__(self, other):
return other + self.as_timedelta()
class Millisecond(Tick):
_freq = "ms"
def as_timedelta(self) -> timedelta:
return timedelta(milliseconds=self.n)
def __apply__(self, other):
return other + self.as_timedelta()
class Microsecond(Tick):
_freq = "us"
def as_timedelta(self) -> timedelta:
return timedelta(microseconds=self.n)
def __apply__(self, other):
return other + self.as_timedelta()
def _generate_anchored_offsets(
base_freq: str, offset: type[YearOffset | QuarterOffset]
) -> dict[str, type[BaseCFTimeOffset]]:
offsets: dict[str, type[BaseCFTimeOffset]] = {}
for month, abbreviation in _MONTH_ABBREVIATIONS.items():
anchored_freq = f"{base_freq}-{abbreviation}"
offsets[anchored_freq] = partial(offset, month=month) # type: ignore[assignment]
return offsets
_FREQUENCIES: Mapping[str, type[BaseCFTimeOffset]] = {
"A": YearEnd,
"AS": YearBegin,
"Y": YearEnd,
"YE": YearEnd,
"YS": YearBegin,
"Q": partial(QuarterEnd, month=12), # type: ignore[dict-item]
"QE": partial(QuarterEnd, month=12), # type: ignore[dict-item]
"QS": partial(QuarterBegin, month=1), # type: ignore[dict-item]
"M": MonthEnd,
"ME": MonthEnd,
"MS": MonthBegin,
"D": Day,
"H": Hour,
"h": Hour,
"T": Minute,
"min": Minute,
"S": Second,
"s": Second,
"L": Millisecond,
"ms": Millisecond,
"U": Microsecond,
"us": Microsecond,
**_generate_anchored_offsets("AS", YearBegin),
**_generate_anchored_offsets("A", YearEnd),
**_generate_anchored_offsets("YS", YearBegin),
**_generate_anchored_offsets("Y", YearEnd),
**_generate_anchored_offsets("YE", YearEnd),
**_generate_anchored_offsets("QS", QuarterBegin),
**_generate_anchored_offsets("Q", QuarterEnd),
**_generate_anchored_offsets("QE", QuarterEnd),
}
_FREQUENCY_CONDITION = "|".join(_FREQUENCIES.keys())
_PATTERN = rf"^((?P<multiple>[+-]?\d+)|())(?P<freq>({_FREQUENCY_CONDITION}))$"
# pandas defines these offsets as "Tick" objects, which for instance have
# distinct behavior from monthly or longer frequencies in resample.
CFTIME_TICKS = (Day, Hour, Minute, Second)
def _generate_anchored_deprecated_frequencies(
deprecated: str, recommended: str
) -> dict[str, str]:
pairs = {}
for abbreviation in _MONTH_ABBREVIATIONS.values():
anchored_deprecated = f"{deprecated}-{abbreviation}"
anchored_recommended = f"{recommended}-{abbreviation}"
pairs[anchored_deprecated] = anchored_recommended
return pairs
_DEPRECATED_FREQUENCIES: dict[str, str] = {
"A": "YE",
"Y": "YE",
"AS": "YS",
"Q": "QE",
"M": "ME",
"H": "h",
"T": "min",
"S": "s",
"L": "ms",
"U": "us",
**_generate_anchored_deprecated_frequencies("A", "YE"),
**_generate_anchored_deprecated_frequencies("Y", "YE"),
**_generate_anchored_deprecated_frequencies("AS", "YS"),
**_generate_anchored_deprecated_frequencies("Q", "QE"),
}
_DEPRECATION_MESSAGE = (
"{deprecated_freq!r} is deprecated and will be removed in a future "
"version. Please use {recommended_freq!r} instead of "
"{deprecated_freq!r}."
)
def _emit_freq_deprecation_warning(deprecated_freq):
recommended_freq = _DEPRECATED_FREQUENCIES[deprecated_freq]
message = _DEPRECATION_MESSAGE.format(
deprecated_freq=deprecated_freq, recommended_freq=recommended_freq
)
emit_user_level_warning(message, FutureWarning)
def to_offset(
freq: BaseCFTimeOffset | str | timedelta | pd.Timedelta | pd.DateOffset,
warn: bool = True,
) -> BaseCFTimeOffset:
"""Convert a frequency string to the appropriate subclass of
BaseCFTimeOffset."""
if isinstance(freq, BaseCFTimeOffset):
return freq
if isinstance(freq, timedelta | pd.Timedelta):
return delta_to_tick(freq)
if isinstance(freq, pd.DateOffset):
freq = _legacy_to_new_freq(freq.freqstr)
match = re.match(_PATTERN, freq)
if match is None:
raise ValueError("Invalid frequency string provided")
freq_data = match.groupdict()
freq = freq_data["freq"]
if warn and freq in _DEPRECATED_FREQUENCIES:
_emit_freq_deprecation_warning(freq)
multiples = freq_data["multiple"]
multiples = 1 if multiples is None else int(multiples)
return _FREQUENCIES[freq](n=multiples)
def delta_to_tick(delta: timedelta | pd.Timedelta) -> Tick:
"""Adapted from pandas.tslib.delta_to_tick"""
if isinstance(delta, pd.Timedelta) and delta.nanoseconds != 0:
# pandas.Timedelta has nanoseconds, but these are not supported
raise ValueError(
"Unable to convert 'pandas.Timedelta' object with non-zero "
"nanoseconds to 'CFTimeOffset' object"
)
if delta.microseconds == 0:
if delta.seconds == 0:
return Day(n=delta.days)
else:
seconds = delta.days * 86400 + delta.seconds
if seconds % 3600 == 0:
return Hour(n=seconds // 3600)
elif seconds % 60 == 0:
return Minute(n=seconds // 60)
else:
return Second(n=seconds)
# Regardless of the days and seconds this will always be a Millisecond
# or Microsecond object
elif delta.microseconds % 1_000 == 0:
return Millisecond(n=delta.microseconds // 1_000)
else:
return Microsecond(n=delta.microseconds)
def to_cftime_datetime(date_str_or_date, calendar=None):
if TYPE_CHECKING:
import cftime
else:
cftime = attempt_import("cftime")
if isinstance(date_str_or_date, str):
if calendar is None:
raise ValueError(
"If converting a string to a cftime.datetime object, "
"a calendar type must be provided"
)
date, _ = _parse_iso8601(get_date_type(calendar), date_str_or_date)
return date
elif isinstance(date_str_or_date, cftime.datetime):
return date_str_or_date
elif isinstance(date_str_or_date, datetime | pd.Timestamp):
return cftime.DatetimeProlepticGregorian(*date_str_or_date.timetuple())
else:
raise TypeError(
"date_str_or_date must be a string or a "
"subclass of cftime.datetime. Instead got "
f"{date_str_or_date!r}."
)
def normalize_date(date):
"""Round datetime down to midnight."""
return date.replace(hour=0, minute=0, second=0, microsecond=0)
def _get_normalized_cfdate(date, calendar, normalize):
"""convert to cf datetime and round down to midnight if normalize."""
if date is None:
return date
cf_date = to_cftime_datetime(date, calendar)
return normalize_date(cf_date) if normalize else cf_date
def _generate_linear_date_range(start, end, periods):
"""Generate an equally-spaced sequence of cftime.datetime objects between
and including two dates (whose length equals the number of periods)."""
if TYPE_CHECKING:
import cftime
else:
cftime = attempt_import("cftime")
total_seconds = (end - start).total_seconds()
values = np.linspace(0.0, total_seconds, periods, endpoint=True)
units = f"seconds since {format_cftime_datetime(start)}"
calendar = start.calendar
return cftime.num2date(
values, units=units, calendar=calendar, only_use_cftime_datetimes=True
)
def _generate_linear_date_range_with_freq(start, end, periods, freq):
"""Generate a regular range of cftime.datetime objects with a
given frequency.
Adapted from pandas.tseries.offsets.generate_range (now at
pandas.core.arrays.datetimes._generate_range).
Parameters
----------
start : cftime.datetime, or None
Start of range
end : cftime.datetime, or None
End of range
periods : int, or None
Number of elements in the sequence
freq: str
Step size between cftime.datetime objects. Not None.
Returns
-------
A generator object of cftime.datetime objects
"""
offset = to_offset(freq)
if start:
# From pandas GH 56147 / 56832 to account for negative direction and
# range bounds
if offset.n >= 0:
start = offset.rollforward(start)
else:
start = offset.rollback(start)
if periods is None and end < start and offset.n >= 0:
end = None
periods = 0
if end is None:
end = start + (periods - 1) * offset
if start is None:
start = end - (periods - 1) * offset
current = start
if offset.n >= 0:
while current <= end:
yield current
next_date = current + offset
if next_date <= current:
raise ValueError(f"Offset {offset} did not increment date")
current = next_date
else:
while current >= end:
yield current
next_date = current + offset
if next_date >= current:
raise ValueError(f"Offset {offset} did not decrement date")
current = next_date
def cftime_range(
start=None,
end=None,
periods=None,
freq=None,
normalize=False,
name=None,
inclusive: InclusiveOptions = "both",
calendar="standard",
) -> CFTimeIndex:
"""Return a fixed frequency CFTimeIndex.
.. deprecated:: 2025.02.0
Use :py:func:`~xarray.date_range` with ``use_cftime=True`` instead.
Parameters
----------
start : str or cftime.datetime, optional
Left bound for generating dates.
end : str or cftime.datetime, optional
Right bound for generating dates.
periods : int, optional
Number of periods to generate.
freq : str or None, default: "D"
Frequency strings can have multiples, e.g. "5h" and negative values, e.g. "-1D".
normalize : bool, default: False
Normalize start/end dates to midnight before generating date range.
name : str, default: None
Name of the resulting index
inclusive : {"both", "neither", "left", "right"}, default "both"
Include boundaries; whether to set each bound as closed or open.
.. versionadded:: 2023.02.0
calendar : str, default: "standard"
Calendar type for the datetimes.
Returns
-------
CFTimeIndex
Notes
-----
This function is an analog of ``pandas.date_range`` for use in generating
sequences of ``cftime.datetime`` objects. It supports most of the
features of ``pandas.date_range`` (e.g. specifying how the index is
``closed`` on either side, or whether or not to ``normalize`` the start and
end bounds); however, there are some notable exceptions:
- You cannot specify a ``tz`` (time zone) argument.
- Start or end dates specified as partial-datetime strings must use the
`ISO-8601 format <https://en.wikipedia.org/wiki/ISO_8601>`_.
- It supports many, but not all, frequencies supported by
``pandas.date_range``. For example it does not currently support any of
the business-related or semi-monthly frequencies.
- Compound sub-monthly frequencies are not supported, e.g. '1H1min', as
these can easily be written in terms of the finest common resolution,
e.g. '61min'.
Valid simple frequency strings for use with ``cftime``-calendars include
any multiples of the following.
+--------+--------------------------+
| Alias | Description |
+========+==========================+
| YE | Year-end frequency |
+--------+--------------------------+
| YS | Year-start frequency |
+--------+--------------------------+
| QE | Quarter-end frequency |
+--------+--------------------------+
| QS | Quarter-start frequency |
+--------+--------------------------+
| ME | Month-end frequency |
+--------+--------------------------+
| MS | Month-start frequency |
+--------+--------------------------+
| D | Day frequency |
+--------+--------------------------+
| h | Hour frequency |
+--------+--------------------------+
| min | Minute frequency |
+--------+--------------------------+
| s | Second frequency |
+--------+--------------------------+
| ms | Millisecond frequency |
+--------+--------------------------+
| us | Microsecond frequency |
+--------+--------------------------+
Any multiples of the following anchored offsets are also supported.
+------------+--------------------------------------------------------------------+
| Alias | Description |
+============+====================================================================+
| Y(E,S)-JAN | Annual frequency, anchored at the (end, beginning) of January |
+------------+--------------------------------------------------------------------+
| Y(E,S)-FEB | Annual frequency, anchored at the (end, beginning) of February |
+------------+--------------------------------------------------------------------+
| Y(E,S)-MAR | Annual frequency, anchored at the (end, beginning) of March |
+------------+--------------------------------------------------------------------+
| Y(E,S)-APR | Annual frequency, anchored at the (end, beginning) of April |
+------------+--------------------------------------------------------------------+
| Y(E,S)-MAY | Annual frequency, anchored at the (end, beginning) of May |
+------------+--------------------------------------------------------------------+
| Y(E,S)-JUN | Annual frequency, anchored at the (end, beginning) of June |
+------------+--------------------------------------------------------------------+
| Y(E,S)-JUL | Annual frequency, anchored at the (end, beginning) of July |
+------------+--------------------------------------------------------------------+
| Y(E,S)-AUG | Annual frequency, anchored at the (end, beginning) of August |
+------------+--------------------------------------------------------------------+
| Y(E,S)-SEP | Annual frequency, anchored at the (end, beginning) of September |
+------------+--------------------------------------------------------------------+
| Y(E,S)-OCT | Annual frequency, anchored at the (end, beginning) of October |
+------------+--------------------------------------------------------------------+
| Y(E,S)-NOV | Annual frequency, anchored at the (end, beginning) of November |
+------------+--------------------------------------------------------------------+
| Y(E,S)-DEC | Annual frequency, anchored at the (end, beginning) of December |
+------------+--------------------------------------------------------------------+
| Q(E,S)-JAN | Quarter frequency, anchored at the (end, beginning) of January |
+------------+--------------------------------------------------------------------+
| Q(E,S)-FEB | Quarter frequency, anchored at the (end, beginning) of February |
+------------+--------------------------------------------------------------------+
| Q(E,S)-MAR | Quarter frequency, anchored at the (end, beginning) of March |
+------------+--------------------------------------------------------------------+
| Q(E,S)-APR | Quarter frequency, anchored at the (end, beginning) of April |
+------------+--------------------------------------------------------------------+
| Q(E,S)-MAY | Quarter frequency, anchored at the (end, beginning) of May |
+------------+--------------------------------------------------------------------+
| Q(E,S)-JUN | Quarter frequency, anchored at the (end, beginning) of June |
+------------+--------------------------------------------------------------------+
| Q(E,S)-JUL | Quarter frequency, anchored at the (end, beginning) of July |
+------------+--------------------------------------------------------------------+
| Q(E,S)-AUG | Quarter frequency, anchored at the (end, beginning) of August |
+------------+--------------------------------------------------------------------+
| Q(E,S)-SEP | Quarter frequency, anchored at the (end, beginning) of September |
+------------+--------------------------------------------------------------------+
| Q(E,S)-OCT | Quarter frequency, anchored at the (end, beginning) of October |
+------------+--------------------------------------------------------------------+
| Q(E,S)-NOV | Quarter frequency, anchored at the (end, beginning) of November |
+------------+--------------------------------------------------------------------+
| Q(E,S)-DEC | Quarter frequency, anchored at the (end, beginning) of December |
+------------+--------------------------------------------------------------------+
Finally, the following calendar aliases are supported.
+--------------------------------+---------------------------------------+
| Alias | Date type |
+================================+=======================================+
| standard, gregorian | ``cftime.DatetimeGregorian`` |
+--------------------------------+---------------------------------------+
| proleptic_gregorian | ``cftime.DatetimeProlepticGregorian`` |
+--------------------------------+---------------------------------------+
| noleap, 365_day | ``cftime.DatetimeNoLeap`` |
+--------------------------------+---------------------------------------+
| all_leap, 366_day | ``cftime.DatetimeAllLeap`` |
+--------------------------------+---------------------------------------+
| 360_day | ``cftime.Datetime360Day`` |
+--------------------------------+---------------------------------------+
| julian | ``cftime.DatetimeJulian`` |
+--------------------------------+---------------------------------------+
Examples
--------
This function returns a ``CFTimeIndex``, populated with ``cftime.datetime``
objects associated with the specified calendar type, e.g.
>>> xr.date_range(
... start="2000", periods=6, freq="2MS", calendar="noleap", use_cftime=True
... )
CFTimeIndex([2000-01-01 00:00:00, 2000-03-01 00:00:00, 2000-05-01 00:00:00,
2000-07-01 00:00:00, 2000-09-01 00:00:00, 2000-11-01 00:00:00],
dtype='object', length=6, calendar='noleap', freq='2MS')
As in the standard pandas function, three of the ``start``, ``end``,
``periods``, or ``freq`` arguments must be specified at a given time, with
the other set to ``None``. See the `pandas documentation
<https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html>`_
for more examples of the behavior of ``date_range`` with each of the
parameters.
See Also
--------
pandas.date_range
"""
emit_user_level_warning(
"cftime_range() is deprecated, please use xarray.date_range(..., use_cftime=True) instead.",
DeprecationWarning,
)
return date_range(
start=start,
end=end,
periods=periods,
freq=freq,
normalize=normalize,
name=name,
inclusive=inclusive,
calendar=calendar,
use_cftime=True,
)
def _cftime_range(
start=None,
end=None,
periods=None,
freq=None,
normalize=False,
name=None,
inclusive: InclusiveOptions = "both",
calendar="standard",
) -> CFTimeIndex:
"""Return a fixed frequency CFTimeIndex.
Parameters
----------
start : str or cftime.datetime, optional
Left bound for generating dates.
end : str or cftime.datetime, optional
Right bound for generating dates.
periods : int, optional
Number of periods to generate.
freq : str or None, default: "D"
Frequency strings can have multiples, e.g. "5h" and negative values, e.g. "-1D".
normalize : bool, default: False
Normalize start/end dates to midnight before generating date range.
name : str, default: None
Name of the resulting index
inclusive : {"both", "neither", "left", "right"}, default "both"
Include boundaries; whether to set each bound as closed or open.
calendar : str, default: "standard"
Calendar type for the datetimes.
Returns
-------
CFTimeIndex
Notes
-----
see cftime_range
"""
if freq is None and any(arg is None for arg in [periods, start, end]):
freq = "D"
# Adapted from pandas.core.indexes.datetimes._generate_range.
if count_not_none(start, end, periods, freq) != 3:
raise ValueError(
"Exactly three of 'start', 'end', 'periods', or 'freq' must be "
"specified to generate a date range. Note that 'freq' defaults to "
"'D' in the event that any of 'start', 'end', or 'periods' are "
"None."
)
start = _get_normalized_cfdate(start, calendar, normalize)
end = _get_normalized_cfdate(end, calendar, normalize)
if freq is None:
dates = _generate_linear_date_range(start, end, periods)
else:
dates = np.array(
list(_generate_linear_date_range_with_freq(start, end, periods, freq))
)
if not TYPE_CHECKING and inclusive not in get_args(InclusiveOptions):
raise ValueError(
f"Argument `inclusive` must be either 'both', 'neither', "
f"'left', or 'right'. Got {inclusive}."
)
if len(dates) and inclusive != "both":
if inclusive != "left" and dates[0] == start:
dates = dates[1:]
if inclusive != "right" and dates[-1] == end:
dates = dates[:-1]
return CFTimeIndex(dates, name=name)
def date_range(
start=None,
end=None,
periods=None,
freq=None,
tz=None,
normalize=False,
name=None,
inclusive: InclusiveOptions = "both",
unit: PDDatetimeUnitOptions = "ns",
calendar="standard",
use_cftime=None,
):
"""Return a fixed frequency datetime index.
The type (:py:class:`xarray.CFTimeIndex` or :py:class:`pandas.DatetimeIndex`)
of the returned index depends on the requested calendar and on `use_cftime`.
Parameters
----------
start : str or datetime-like, optional
Left bound for generating dates.
end : str or datetime-like, optional
Right bound for generating dates.
periods : int, optional
Number of periods to generate.
freq : str or None, default: "D"
Frequency strings can have multiples, e.g. "5h" and negative values, e.g. "-1D".
tz : str or tzinfo, optional
Time zone name for returning localized DatetimeIndex, for example
'Asia/Hong_Kong'. By default, the resulting DatetimeIndex is
timezone-naive. Only valid with pandas DatetimeIndex.
normalize : bool, default: False
Normalize start/end dates to midnight before generating date range.
name : str, default: None
Name of the resulting index
inclusive : {"both", "neither", "left", "right"}, default: "both"
Include boundaries; whether to set each bound as closed or open.
.. versionadded:: 2023.02.0
unit : {"s", "ms", "us", "ns"}, default "ns"
Specify the desired resolution of the result.
.. versionadded:: 2024.12.0
calendar : str, default: "standard"
Calendar type for the datetimes.
use_cftime : boolean, optional
If True, always return a CFTimeIndex.
If False, return a pd.DatetimeIndex if possible or raise a ValueError.
If None (default), return a pd.DatetimeIndex if possible,
otherwise return a CFTimeIndex. Overridden to False if `tz` is not None.
Returns
-------
CFTimeIndex or pd.DatetimeIndex
Notes
-----
When ``use_cftime=True``, or a calendar other than "standard", "gregorian",
or "proleptic_gregorian" is provided, this function is an analog of ``pandas.date_range``
for use in generating sequences of ``cftime.datetime`` objects. It supports most of the
features of ``pandas.date_range`` (e.g. specifying how the index is
``closed`` on either side, or whether or not to ``normalize`` the start and
end bounds); however, there are some notable exceptions:
- You cannot specify a ``tz`` (time zone) argument.
- Start or end dates specified as partial-datetime strings must use the
`ISO-8601 format <https://en.wikipedia.org/wiki/ISO_8601>`_.
- It supports many, but not all, frequencies supported by
``pandas.date_range``. For example it does not currently support any of
the business-related or semi-monthly frequencies.
- Compound sub-monthly frequencies are not supported, e.g. '1H1min', as
these can easily be written in terms of the finest common resolution,
e.g. '61min'.
Valid simple frequency strings for use with ``cftime``-calendars include
any multiples of the following.
+--------+--------------------------+
| Alias | Description |
+========+==========================+
| YE | Year-end frequency |
+--------+--------------------------+
| YS | Year-start frequency |
+--------+--------------------------+
| QE | Quarter-end frequency |
+--------+--------------------------+
| QS | Quarter-start frequency |
+--------+--------------------------+
| ME | Month-end frequency |
+--------+--------------------------+
| MS | Month-start frequency |
+--------+--------------------------+
| D | Day frequency |
+--------+--------------------------+
| h | Hour frequency |
+--------+--------------------------+
| min | Minute frequency |
+--------+--------------------------+
| s | Second frequency |
+--------+--------------------------+
| ms | Millisecond frequency |
+--------+--------------------------+
| us | Microsecond frequency |
+--------+--------------------------+
Any multiples of the following anchored offsets are also supported.
+------------+--------------------------------------------------------------------+
| Alias | Description |
+============+====================================================================+
| Y(E,S)-JAN | Annual frequency, anchored at the (end, beginning) of January |
+------------+--------------------------------------------------------------------+
| Y(E,S)-FEB | Annual frequency, anchored at the (end, beginning) of February |
+------------+--------------------------------------------------------------------+
| Y(E,S)-MAR | Annual frequency, anchored at the (end, beginning) of March |
+------------+--------------------------------------------------------------------+
| Y(E,S)-APR | Annual frequency, anchored at the (end, beginning) of April |
+------------+--------------------------------------------------------------------+
| Y(E,S)-MAY | Annual frequency, anchored at the (end, beginning) of May |
+------------+--------------------------------------------------------------------+
| Y(E,S)-JUN | Annual frequency, anchored at the (end, beginning) of June |
+------------+--------------------------------------------------------------------+
| Y(E,S)-JUL | Annual frequency, anchored at the (end, beginning) of July |
+------------+--------------------------------------------------------------------+
| Y(E,S)-AUG | Annual frequency, anchored at the (end, beginning) of August |
+------------+--------------------------------------------------------------------+
| Y(E,S)-SEP | Annual frequency, anchored at the (end, beginning) of September |
+------------+--------------------------------------------------------------------+
| Y(E,S)-OCT | Annual frequency, anchored at the (end, beginning) of October |
+------------+--------------------------------------------------------------------+
| Y(E,S)-NOV | Annual frequency, anchored at the (end, beginning) of November |
+------------+--------------------------------------------------------------------+
| Y(E,S)-DEC | Annual frequency, anchored at the (end, beginning) of December |
+------------+--------------------------------------------------------------------+
| Q(E,S)-JAN | Quarter frequency, anchored at the (end, beginning) of January |
+------------+--------------------------------------------------------------------+
| Q(E,S)-FEB | Quarter frequency, anchored at the (end, beginning) of February |
+------------+--------------------------------------------------------------------+
| Q(E,S)-MAR | Quarter frequency, anchored at the (end, beginning) of March |
+------------+--------------------------------------------------------------------+
| Q(E,S)-APR | Quarter frequency, anchored at the (end, beginning) of April |
+------------+--------------------------------------------------------------------+
| Q(E,S)-MAY | Quarter frequency, anchored at the (end, beginning) of May |
+------------+--------------------------------------------------------------------+
| Q(E,S)-JUN | Quarter frequency, anchored at the (end, beginning) of June |
+------------+--------------------------------------------------------------------+
| Q(E,S)-JUL | Quarter frequency, anchored at the (end, beginning) of July |
+------------+--------------------------------------------------------------------+
| Q(E,S)-AUG | Quarter frequency, anchored at the (end, beginning) of August |
+------------+--------------------------------------------------------------------+
| Q(E,S)-SEP | Quarter frequency, anchored at the (end, beginning) of September |
+------------+--------------------------------------------------------------------+
| Q(E,S)-OCT | Quarter frequency, anchored at the (end, beginning) of October |
+------------+--------------------------------------------------------------------+
| Q(E,S)-NOV | Quarter frequency, anchored at the (end, beginning) of November |
+------------+--------------------------------------------------------------------+
| Q(E,S)-DEC | Quarter frequency, anchored at the (end, beginning) of December |
+------------+--------------------------------------------------------------------+
Finally, the following calendar aliases are supported.
+--------------------------------+---------------------------------------+----------------------------+
| Alias | Date type | Available use_cftime=False |
+================================+=======================================+============================+
| standard, gregorian | ``cftime.DatetimeGregorian`` | True |
+--------------------------------+---------------------------------------+----------------------------+
| proleptic_gregorian | ``cftime.DatetimeProlepticGregorian`` | True |
+--------------------------------+---------------------------------------+----------------------------+
| noleap, 365_day | ``cftime.DatetimeNoLeap`` | False |
+--------------------------------+---------------------------------------+----------------------------+
| all_leap, 366_day | ``cftime.DatetimeAllLeap`` | False |
+--------------------------------+---------------------------------------+----------------------------+
| 360_day | ``cftime.Datetime360Day`` | False |
+--------------------------------+---------------------------------------+----------------------------+
| julian | ``cftime.DatetimeJulian`` | False |
+--------------------------------+---------------------------------------+----------------------------+
As in the standard pandas function, exactly three of ``start``, ``end``,
``periods``, or ``freq`` are required to generate a date range. Note that
``freq`` defaults to ``"D"`` in the event that any of ``start``, ``end``,
or ``periods`` are set to ``None``. See :py:func:`pandas.date_range`.
for more examples of the behavior of ``date_range`` with each of the
parameters.
Examples
--------
This function returns a ``CFTimeIndex``, populated with ``cftime.datetime``
objects associated with the specified calendar type, e.g.
>>> xr.date_range(
... start="2000", periods=6, freq="2MS", calendar="noleap", use_cftime=True
... )
CFTimeIndex([2000-01-01 00:00:00, 2000-03-01 00:00:00, 2000-05-01 00:00:00,
2000-07-01 00:00:00, 2000-09-01 00:00:00, 2000-11-01 00:00:00],
dtype='object', length=6, calendar='noleap', freq='2MS')
See also
--------
pandas.date_range
cftime_range
date_range_like
"""
if tz is not None:
use_cftime = False
if _is_standard_calendar(calendar) and use_cftime is not True:
try:
return pd.date_range(
start=start,
end=end,
periods=periods,
# TODO remove translation once requiring pandas >= 2.2
freq=_new_to_legacy_freq(freq),
tz=tz,
normalize=normalize,
name=name,
inclusive=inclusive,
unit=unit,
)
except pd.errors.OutOfBoundsDatetime as err:
if use_cftime is False:
raise ValueError(
"Date range is invalid for pandas DatetimeIndex, try using `use_cftime=True`."
) from err
elif use_cftime is False:
raise ValueError(
f"Invalid calendar {calendar} for pandas DatetimeIndex, try using `use_cftime=True`."
)
return _cftime_range(
start=start,
end=end,
periods=periods,
freq=freq,
normalize=normalize,
name=name,
inclusive=inclusive,
calendar=calendar,
)
def _new_to_legacy_freq(freq):
# xarray will now always return "ME" and "QE" for MonthEnd and QuarterEnd
# frequencies, but older versions of pandas do not support these as
# frequency strings. Until xarray's minimum pandas version is 2.2 or above,
# we add logic to continue using the deprecated "M" and "Q" frequency
# strings in these circumstances.
# NOTE: other conversions ("h" -> "H", ..., "ns" -> "N") not required
# TODO: remove once requiring pandas >= 2.2
if not freq or Version(pd.__version__) >= Version("2.2"):
return freq
try:
freq_as_offset = to_offset(freq)
except ValueError:
# freq may be valid in pandas but not in xarray
return freq
if isinstance(freq_as_offset, MonthEnd) and "ME" in freq:
freq = freq.replace("ME", "M")
elif isinstance(freq_as_offset, QuarterEnd) and "QE" in freq:
freq = freq.replace("QE", "Q")
elif isinstance(freq_as_offset, YearBegin) and "YS" in freq:
freq = freq.replace("YS", "AS")
elif isinstance(freq_as_offset, YearEnd):
# testing for "Y" is required as this was valid in xarray 2023.11 - 2024.01
if "Y-" in freq:
# Check for and replace "Y-" instead of just "Y" to prevent
# corrupting anchored offsets that contain "Y" in the month
# abbreviation, e.g. "Y-MAY" -> "A-MAY".
freq = freq.replace("Y-", "A-")
elif "YE-" in freq:
freq = freq.replace("YE-", "A-")
elif "A-" not in freq and freq.endswith("Y"):
freq = freq.replace("Y", "A")
elif freq.endswith("YE"):
freq = freq.replace("YE", "A")
return freq
def _legacy_to_new_freq(freq: T_FreqStr) -> T_FreqStr:
# to avoid internal deprecation warnings when freq is determined using pandas < 2.2
# TODO: remove once requiring pandas >= 2.2
if not freq or Version(pd.__version__) >= Version("2.2"):
return freq
try:
freq_as_offset = to_offset(freq, warn=False)
except ValueError:
# freq may be valid in pandas but not in xarray
return freq
if isinstance(freq_as_offset, MonthEnd) and "ME" not in freq:
freq = freq.replace("M", "ME")
elif isinstance(freq_as_offset, QuarterEnd) and "QE" not in freq:
freq = freq.replace("Q", "QE")
elif isinstance(freq_as_offset, YearBegin) and "YS" not in freq:
freq = freq.replace("AS", "YS")
elif isinstance(freq_as_offset, YearEnd):
if "A-" in freq:
# Check for and replace "A-" instead of just "A" to prevent
# corrupting anchored offsets that contain "Y" in the month
# abbreviation, e.g. "A-MAY" -> "YE-MAY".
freq = freq.replace("A-", "YE-")
elif "Y-" in freq:
freq = freq.replace("Y-", "YE-")
elif freq.endswith("A"):
# the "A-MAY" case is already handled above
freq = freq.replace("A", "YE")
elif "YE" not in freq and freq.endswith("Y"):
# the "Y-MAY" case is already handled above
freq = freq.replace("Y", "YE")
elif isinstance(freq_as_offset, Hour):
freq = freq.replace("H", "h")
elif isinstance(freq_as_offset, Minute):
freq = freq.replace("T", "min")
elif isinstance(freq_as_offset, Second):
freq = freq.replace("S", "s")
elif isinstance(freq_as_offset, Millisecond):
freq = freq.replace("L", "ms")
elif isinstance(freq_as_offset, Microsecond):
freq = freq.replace("U", "us")
return freq
def date_range_like(source, calendar, use_cftime=None):
"""Generate a datetime array with the same frequency, start and end as
another one, but in a different calendar.
Parameters
----------
source : DataArray, CFTimeIndex, or pd.DatetimeIndex
1D datetime array
calendar : str
New calendar name.
use_cftime : bool, optional
If True, the output uses :py:class:`cftime.datetime` objects.
If None (default), :py:class:`numpy.datetime64` values are used if possible.
If False, :py:class:`numpy.datetime64` values are used or an error is raised.
Returns
-------
DataArray
1D datetime coordinate with the same start, end and frequency as the
source, but in the new calendar. The start date is assumed to exist in
the target calendar. If the end date doesn't exist, the code tries 1
and 2 calendar days before. There is a special case when the source time
series is daily or coarser and the end of the input range is on the
last day of the month. Then the output range will also end on the last
day of the month in the new calendar.
"""
from xarray.coding.frequencies import infer_freq
from xarray.core.dataarray import DataArray
if not isinstance(source, pd.DatetimeIndex | CFTimeIndex) and (
(isinstance(source, DataArray) and (source.ndim != 1))
or not _contains_datetime_like_objects(source.variable)
):
raise ValueError(
"'source' must be a 1D array of datetime objects for inferring its range."
)
freq = infer_freq(source)
if freq is None:
raise ValueError(
"`date_range_like` was unable to generate a range as the source frequency was not inferable."
)
# TODO remove once requiring pandas >= 2.2
freq = _legacy_to_new_freq(freq)
use_cftime = _should_cftime_be_used(source, calendar, use_cftime)
source_start = source.values.min()
source_end = source.values.max()
freq_as_offset = to_offset(freq)
if freq_as_offset.n < 0:
source_start, source_end = source_end, source_start
if is_np_datetime_like(source.dtype):
# We want to use datetime fields (datetime64 object don't have them)
source_calendar = "standard"
source_start = default_precision_timestamp(source_start)
source_end = default_precision_timestamp(source_end)
elif isinstance(source, CFTimeIndex):
source_calendar = source.calendar
else: # DataArray
source_calendar = source.dt.calendar
if calendar == source_calendar and is_np_datetime_like(source.dtype) ^ use_cftime:
return source
date_type = get_date_type(calendar, use_cftime)
start = convert_time_or_go_back(source_start, date_type)
end = convert_time_or_go_back(source_end, date_type)
# For the cases where the source ends on the end of the month, we expect the same in the new calendar.
if source_end.day == source_end.daysinmonth and isinstance(
freq_as_offset, YearEnd | QuarterEnd | MonthEnd | Day
):
end = end.replace(day=end.daysinmonth)
return date_range(
start=start.isoformat(),
end=end.isoformat(),
freq=freq,
calendar=calendar,
)
|