@@ -445,7 +445,7 @@ def _generate_range( # type: ignore[override]
445445 i8values = generate_regular_range (start , end , periods , freq , unit = unit )
446446 else :
447447 xdr = _generate_range (
448- start = start , end = end , periods = periods , offset = freq
448+ start = start , end = end , periods = periods , offset = freq , unit = unit
449449 )
450450 i8values = np .array ([x .value for x in xdr ], dtype = np .int64 )
451451
@@ -508,7 +508,10 @@ def _unbox_scalar(self, value) -> np.datetime64:
508508 if not isinstance (value , self ._scalar_type ) and value is not NaT :
509509 raise ValueError ("'value' should be a Timestamp." )
510510 self ._check_compatible_with (value )
511- return value .asm8
511+ if value is NaT :
512+ return np .datetime64 (value .value , self .unit )
513+ else :
514+ return value .as_unit (self .unit ).asm8
512515
513516 def _scalar_from_string (self , value ) -> Timestamp | NaTType :
514517 return Timestamp (value , tz = self .tz )
@@ -2475,6 +2478,8 @@ def _generate_range(
24752478 end : Timestamp | None ,
24762479 periods : int | None ,
24772480 offset : BaseOffset ,
2481+ * ,
2482+ unit : str ,
24782483):
24792484 """
24802485 Generates a sequence of dates corresponding to the specified time
@@ -2486,7 +2491,8 @@ def _generate_range(
24862491 start : Timestamp or None
24872492 end : Timestamp or None
24882493 periods : int or None
2489- offset : DateOffset,
2494+ offset : DateOffset
2495+ unit : str
24902496
24912497 Notes
24922498 -----
@@ -2506,13 +2512,20 @@ def _generate_range(
25062512 start = Timestamp (start ) # type: ignore[arg-type]
25072513 # Non-overlapping identity check (left operand type: "Timestamp", right
25082514 # operand type: "NaTType")
2509- start = start if start is not NaT else None # type: ignore[comparison-overlap]
2515+ if start is not NaT : # type: ignore[comparison-overlap]
2516+ start = start .as_unit (unit )
2517+ else :
2518+ start = None
2519+
25102520 # Argument 1 to "Timestamp" has incompatible type "Optional[Timestamp]";
25112521 # expected "Union[integer[Any], float, str, date, datetime64]"
25122522 end = Timestamp (end ) # type: ignore[arg-type]
25132523 # Non-overlapping identity check (left operand type: "Timestamp", right
25142524 # operand type: "NaTType")
2515- end = end if end is not NaT else None # type: ignore[comparison-overlap]
2525+ if end is not NaT : # type: ignore[comparison-overlap]
2526+ end = end .as_unit (unit )
2527+ else :
2528+ end = None
25162529
25172530 if start and not offset .is_on_offset (start ):
25182531 # Incompatible types in assignment (expression has type "datetime",
@@ -2553,7 +2566,7 @@ def _generate_range(
25532566 break
25542567
25552568 # faster than cur + offset
2556- next_date = offset ._apply (cur )
2569+ next_date = offset ._apply (cur ). as_unit ( unit )
25572570 if next_date <= cur :
25582571 raise ValueError (f"Offset { offset } did not increment date" )
25592572 cur = next_date
@@ -2567,7 +2580,7 @@ def _generate_range(
25672580 break
25682581
25692582 # faster than cur + offset
2570- next_date = offset ._apply (cur )
2583+ next_date = offset ._apply (cur ). as_unit ( unit )
25712584 if next_date >= cur :
25722585 raise ValueError (f"Offset { offset } did not decrement date" )
25732586 cur = next_date
0 commit comments