5
5
import pandas ._tseries as lib
6
6
import re
7
7
8
- from pandas ._tseries import Timestamp , monthrange
8
+ from pandas ._tseries import Timestamp
9
+ import pandas .core .common as com
9
10
10
11
try :
11
12
import dateutil
@@ -65,15 +66,33 @@ def parser(x):
65
66
data = p_ufunc (arr )
66
67
return np .array (data , dtype = 'M8[us]' )
67
68
68
- def to_datetime (arg ):
69
- """Attempts to convert arg to datetime"""
69
+ def to_datetime (arg , errors = 'ignore' ):
70
+ """
71
+ Convert argument to datetime
72
+
73
+ Parameters
74
+ ----------
75
+ arg : string, datetime, array of strings (with possible NAs)
76
+ errors : {'ignore', 'raise'}, default 'ignore'
77
+ Errors are ignored by default (values left untouched)
78
+
79
+ Returns
80
+ -------
81
+ ret : datetime if parsing succeeded
82
+ """
70
83
if arg is None :
71
84
return arg
72
85
elif isinstance (arg , datetime ):
73
86
return arg
87
+ elif isinstance (arg , np .ndarray ):
88
+ return lib .string_to_datetime (com ._ensure_object (arg ),
89
+ raise_ = errors == 'raise' )
90
+
74
91
try :
75
92
return parser .parse (arg )
76
93
except Exception :
94
+ if errors == 'raise' :
95
+ raise
77
96
return arg
78
97
79
98
@@ -1151,7 +1170,7 @@ class MonthEnd(DateOffset, CacheableOffset):
1151
1170
1152
1171
def apply (self , other ):
1153
1172
n = self .n
1154
- _ , days_in_month = monthrange (other .year , other .month )
1173
+ _ , days_in_month = lib . monthrange (other .year , other .month )
1155
1174
if other .day != days_in_month :
1156
1175
other = other + relativedelta (months = - 1 , day = 31 )
1157
1176
if n <= 0 :
@@ -1161,8 +1180,8 @@ def apply(self, other):
1161
1180
1162
1181
@classmethod
1163
1182
def onOffset (cls , someDate ):
1164
- __junk , days_in_month = monthrange (someDate .year ,
1165
- someDate .month )
1183
+ __junk , days_in_month = lib . monthrange (someDate .year ,
1184
+ someDate .month )
1166
1185
return someDate .day == days_in_month
1167
1186
1168
1187
def rule_code (self ):
@@ -1184,7 +1203,7 @@ def apply(self, other):
1184
1203
1185
1204
@classmethod
1186
1205
def onOffset (cls , someDate ):
1187
- firstDay , _ = monthrange (someDate .year , someDate .month )
1206
+ firstDay , _ = lib . monthrange (someDate .year , someDate .month )
1188
1207
return someDate .day == (firstDay + 1 )
1189
1208
1190
1209
def rule_code (self ):
@@ -1202,7 +1221,7 @@ def isAnchored(self):
1202
1221
def apply (self , other ):
1203
1222
n = self .n
1204
1223
1205
- wkday , days_in_month = monthrange (other .year , other .month )
1224
+ wkday , days_in_month = lib . monthrange (other .year , other .month )
1206
1225
lastBDay = days_in_month - max (((wkday + days_in_month - 1 ) % 7 ) - 4 , 0 )
1207
1226
1208
1227
if n > 0 and not other .day >= lastBDay :
@@ -1227,15 +1246,15 @@ class BMonthBegin(DateOffset, CacheableOffset):
1227
1246
def apply (self , other ):
1228
1247
n = self .n
1229
1248
1230
- wkday , _ = monthrange (other .year , other .month )
1249
+ wkday , _ = lib . monthrange (other .year , other .month )
1231
1250
firstBDay = _get_firstbday (wkday )
1232
1251
1233
1252
if other .day > firstBDay and n <= 0 :
1234
1253
# as if rolled forward already
1235
1254
n += 1
1236
1255
1237
1256
other = other + relativedelta (months = n )
1238
- wkday , _ = monthrange (other .year , other .month )
1257
+ wkday , _ = lib . monthrange (other .year , other .month )
1239
1258
firstBDay = _get_firstbday (wkday )
1240
1259
result = datetime (other .year , other .month , firstBDay )
1241
1260
return result
@@ -1403,7 +1422,7 @@ def isAnchored(self):
1403
1422
def apply (self , other ):
1404
1423
n = self .n
1405
1424
1406
- wkday , days_in_month = monthrange (other .year , other .month )
1425
+ wkday , days_in_month = lib . monthrange (other .year , other .month )
1407
1426
lastBDay = days_in_month - max (((wkday + days_in_month - 1 ) % 7 ) - 4 , 0 )
1408
1427
1409
1428
monthsToGo = 3 - ((other .month - self .startingMonth ) % 3 )
@@ -1465,7 +1484,7 @@ def apply(self, other):
1465
1484
if self ._normalizeFirst :
1466
1485
other = normalize_date (other )
1467
1486
1468
- wkday , _ = monthrange (other .year , other .month )
1487
+ wkday , _ = lib . monthrange (other .year , other .month )
1469
1488
1470
1489
firstBDay = _get_firstbday (wkday )
1471
1490
@@ -1485,7 +1504,7 @@ def apply(self, other):
1485
1504
1486
1505
# get the first bday for result
1487
1506
other = other + relativedelta (months = 3 * n - monthsSince )
1488
- wkday , _ = monthrange (other .year , other .month )
1507
+ wkday , _ = lib . monthrange (other .year , other .month )
1489
1508
firstBDay = _get_firstbday (wkday )
1490
1509
result = datetime (other .year , other .month , firstBDay )
1491
1510
return result
@@ -1517,7 +1536,7 @@ def isAnchored(self):
1517
1536
def apply (self , other ):
1518
1537
n = self .n
1519
1538
1520
- wkday , days_in_month = monthrange (other .year , other .month )
1539
+ wkday , days_in_month = lib . monthrange (other .year , other .month )
1521
1540
1522
1541
monthsToGo = 3 - ((other .month - self .startingMonth ) % 3 )
1523
1542
if monthsToGo == 3 :
@@ -1556,7 +1575,7 @@ def isAnchored(self):
1556
1575
def apply (self , other ):
1557
1576
n = self .n
1558
1577
1559
- wkday , days_in_month = monthrange (other .year , other .month )
1578
+ wkday , days_in_month = lib . monthrange (other .year , other .month )
1560
1579
1561
1580
monthsSince = (other .month - self .startingMonth ) % 3
1562
1581
@@ -1598,7 +1617,7 @@ def apply(self, other):
1598
1617
if self ._normalizeFirst :
1599
1618
other = normalize_date (other )
1600
1619
1601
- wkday , days_in_month = monthrange (other .year , self .month )
1620
+ wkday , days_in_month = lib . monthrange (other .year , self .month )
1602
1621
lastBDay = (days_in_month -
1603
1622
max (((wkday + days_in_month - 1 ) % 7 ) - 4 , 0 ))
1604
1623
@@ -1614,7 +1633,7 @@ def apply(self, other):
1614
1633
1615
1634
other = other + relativedelta (years = years )
1616
1635
1617
- _ , days_in_month = monthrange (other .year , self .month )
1636
+ _ , days_in_month = lib . monthrange (other .year , self .month )
1618
1637
result = datetime (other .year , self .month , days_in_month )
1619
1638
1620
1639
if result .weekday () > 4 :
@@ -1646,7 +1665,7 @@ def apply(self, other):
1646
1665
if self ._normalizeFirst :
1647
1666
other = normalize_date (other )
1648
1667
1649
- wkday , days_in_month = monthrange (other .year , self .month )
1668
+ wkday , days_in_month = lib . monthrange (other .year , self .month )
1650
1669
1651
1670
firstBDay = _get_firstbday (wkday )
1652
1671
@@ -1664,7 +1683,7 @@ def apply(self, other):
1664
1683
1665
1684
# set first bday for result
1666
1685
other = other + relativedelta (years = years )
1667
- wkday , days_in_month = monthrange (other .year , self .month )
1686
+ wkday , days_in_month = lib . monthrange (other .year , self .month )
1668
1687
firstBDay = _get_firstbday (wkday )
1669
1688
result = datetime (other .year , self .month , firstBDay )
1670
1689
return result
@@ -1688,7 +1707,7 @@ def __init__(self, n=1, **kwds):
1688
1707
1689
1708
def apply (self , other ):
1690
1709
n = self .n
1691
- wkday , days_in_month = monthrange (other .year , self .month )
1710
+ wkday , days_in_month = lib . monthrange (other .year , self .month )
1692
1711
if other .month != self .month or other .day != days_in_month :
1693
1712
other = datetime (other .year - 1 , self .month , days_in_month )
1694
1713
if n <= 0 :
@@ -1697,7 +1716,7 @@ def apply(self, other):
1697
1716
return other
1698
1717
1699
1718
def onOffset (self , someDate ):
1700
- wkday , days_in_month = monthrange (someDate .year , self .month )
1719
+ wkday , days_in_month = lib . monthrange (someDate .year , self .month )
1701
1720
return self .month == someDate .month and someDate .day == days_in_month
1702
1721
1703
1722
def rule_code (self ):
@@ -1826,6 +1845,20 @@ def rule_code(self):
1826
1845
_offset_map = {
1827
1846
"WEEKDAY" : BDay (1 ),
1828
1847
1848
+ # Annual - Calendar
1849
+ "A-JAN" : YearEnd (month = 1 ),
1850
+ "A-FEB" : YearEnd (month = 2 ),
1851
+ "A-MAR" : YearEnd (month = 3 ),
1852
+ "A-APR" : YearEnd (month = 4 ),
1853
+ "A-MAY" : YearEnd (month = 5 ),
1854
+ "A-JUN" : YearEnd (month = 6 ),
1855
+ "A-JUL" : YearEnd (month = 7 ),
1856
+ "A-AUG" : YearEnd (month = 8 ),
1857
+ "A-SEP" : YearEnd (month = 9 ),
1858
+ "A-OCT" : YearEnd (month = 10 ),
1859
+ "A-NOV" : YearEnd (month = 11 ),
1860
+ "A-DEC" : YearEnd (month = 12 ),
1861
+
1829
1862
# Annual - Calendar
1830
1863
"A@JAN" : YearEnd (month = 1 ),
1831
1864
"A@FEB" : YearEnd (month = 2 ),
0 commit comments