Skip to content

Commit 69cd744

Browse files
authored
BUG: handle immutable arrays in tz_convert_from_utc (#35530) (#35532)
1 parent b827fe2 commit 69cd744

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Categorical
5959

6060
Datetimelike
6161
^^^^^^^^^^^^
62-
-
62+
- Bug in :attr:`DatetimeArray.date` where a ``ValueError`` would be raised with a read-only backing array (:issue:`33530`)
6363
-
6464

6565
Timedelta

pandas/_libs/tslibs/tzconversion.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ cpdef int64_t tz_convert_from_utc_single(int64_t val, tzinfo tz):
410410
return val + deltas[pos]
411411

412412

413-
def tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
413+
def tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
414414
"""
415415
Convert the values (in i8) from UTC to tz
416416
@@ -435,7 +435,7 @@ def tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
435435

436436
@cython.boundscheck(False)
437437
@cython.wraparound(False)
438-
cdef int64_t[:] _tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
438+
cdef int64_t[:] _tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
439439
"""
440440
Convert the given values (in i8) either to UTC or from UTC.
441441
@@ -457,7 +457,7 @@ cdef int64_t[:] _tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
457457
str typ
458458

459459
if is_utc(tz):
460-
converted = vals
460+
converted = vals.copy()
461461
elif is_tzlocal(tz):
462462
converted = np.empty(n, dtype=np.int64)
463463
for i in range(n):

pandas/tests/tslibs/test_conversion.py

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ def test_tz_convert_corner(arr):
7878
tm.assert_numpy_array_equal(result, arr)
7979

8080

81+
def test_tz_convert_readonly():
82+
# GH#35530
83+
arr = np.array([0], dtype=np.int64)
84+
arr.setflags(write=False)
85+
result = tzconversion.tz_convert_from_utc(arr, UTC)
86+
tm.assert_numpy_array_equal(result, arr)
87+
88+
8189
@pytest.mark.parametrize("copy", [True, False])
8290
@pytest.mark.parametrize("dtype", ["M8[ns]", "M8[s]"])
8391
def test_length_zero_copy(dtype, copy):

0 commit comments

Comments
 (0)