From bbe89f072cf2fbeaed6a5c37b5169ce75a2923bf Mon Sep 17 00:00:00 2001
From: David Li
Date: Mon, 3 Aug 2020 16:06:32 -0400
Subject: [PATCH] BUG: handle immutable arrays in tz_convert_from_utc (#35530)
---
doc/source/whatsnew/v1.2.0.rst | 2 +-
pandas/_libs/tslibs/tzconversion.pyx | 6 +++---
pandas/tests/tslibs/test_conversion.py | 8 ++++++++
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst
index b16ca0a80c5b4..304897edbb75e 100644
--- a/doc/source/whatsnew/v1.2.0.rst
+++ b/doc/source/whatsnew/v1.2.0.rst
@@ -59,7 +59,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
--
+- Bug in :attr:`DatetimeArray.date` where a ``ValueError`` would be raised with a read-only backing array (:issue:`33530`)
-
Timedelta
diff --git a/pandas/_libs/tslibs/tzconversion.pyx b/pandas/_libs/tslibs/tzconversion.pyx
index 2b148cd8849f1..4c62b16d430bd 100644
--- a/pandas/_libs/tslibs/tzconversion.pyx
+++ b/pandas/_libs/tslibs/tzconversion.pyx
@@ -410,7 +410,7 @@ cpdef int64_t tz_convert_from_utc_single(int64_t val, tzinfo tz):
return val + deltas[pos]
-def tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
+def tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
"""
Convert the values (in i8) from UTC to tz
@@ -435,7 +435,7 @@ def tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
@cython.boundscheck(False)
@cython.wraparound(False)
-cdef int64_t[:] _tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
+cdef int64_t[:] _tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
"""
Convert the given values (in i8) either to UTC or from UTC.
@@ -457,7 +457,7 @@ cdef int64_t[:] _tz_convert_from_utc(int64_t[:] vals, tzinfo tz):
str typ
if is_utc(tz):
- converted = vals
+ converted = vals.copy()
elif is_tzlocal(tz):
converted = np.empty(n, dtype=np.int64)
for i in range(n):
diff --git a/pandas/tests/tslibs/test_conversion.py b/pandas/tests/tslibs/test_conversion.py
index 4f184b78f34a1..87cd97f853f4d 100644
--- a/pandas/tests/tslibs/test_conversion.py
+++ b/pandas/tests/tslibs/test_conversion.py
@@ -78,6 +78,14 @@ def test_tz_convert_corner(arr):
tm.assert_numpy_array_equal(result, arr)
+def test_tz_convert_readonly():
+ # GH#35530
+ arr = np.array([0], dtype=np.int64)
+ arr.setflags(write=False)
+ result = tzconversion.tz_convert_from_utc(arr, UTC)
+ tm.assert_numpy_array_equal(result, arr)
+
+
@pytest.mark.parametrize("copy", [True, False])
@pytest.mark.parametrize("dtype", ["M8[ns]", "M8[s]"])
def test_length_zero_copy(dtype, copy):