Repository: dask/dask

Issue:
Deprecate Series.view
Same as the others, that's deprecated in pandas for 3.0 so we can move ahead with it already



Discussion:
GPUtester: Can one of the admins verify this patch?

Admins can comment `ok to test` to allow this one PR to run or `add to allowlist` to allow all future PRs from the same author to run.
phofl: You'll have to adjust _convert_to_numeric like I did in https://github.com/dask/dask/pull/10749/files

Patch:
diff --git a/dask/dataframe/core.py b/dask/dataframe/core.py
index e1e7904a4..9ac54c8ae 100644
--- a/dask/dataframe/core.py
+++ b/dask/dataframe/core.py
@@ -4652,8 +4652,17 @@ Dask Name: {name}, {layers}""".format(
             token="monotonic_decreasing",
         )
 
+    @_deprecated(
+        message=(
+            "Will be removed in a future version. "
+            "Use `Series.astype()` as an alternative to change the dtype."
+        )
+    )
     @derived_from(pd.Series)
     def view(self, dtype):
+        return self._view(dtype)
+
+    def _view(self, dtype):
         meta = self._meta.view(dtype)
         return self.map_partitions(M.view, dtype, meta=meta)
 
@@ -8721,11 +8730,19 @@ def series_map(base_series, map_series):
 
 
 def _convert_to_numeric(series, skipna):
-    if skipna:
-        return series.dropna().view("i8")
+    if PANDAS_GE_200:
+        if skipna:
+            return series.dropna().astype("i8")
+
+        # series.view("i8") with pd.NaT produces -9223372036854775808 is why we need to do this
+        return series.astype("i8").mask(series.isnull(), np.nan)
+    else:
+        view = "_view" if isinstance(series, Series) else "view"
+        if skipna:
+            return getattr(series.dropna(), view)("i8")
 
-    # series.view("i8") with pd.NaT produces -9223372036854775808 is why we need to do this
-    return series.view("i8").mask(series.isnull(), np.nan)
+        # series.view("i8") with pd.NaT produces -9223372036854775808 is why we need to do this
+        return getattr(series, view)("i8").mask(series.isnull(), np.nan)
 
 
 def _sqrt_and_convert_to_timedelta(partition, axis, dtype=None, *args, **kwargs):


Test patch:
diff --git a/dask/dataframe/tests/test_dataframe.py b/dask/dataframe/tests/test_dataframe.py
index 82fa757db..a9e39b424 100644
--- a/dask/dataframe/tests/test_dataframe.py
+++ b/dask/dataframe/tests/test_dataframe.py
@@ -5673,8 +5673,11 @@ def test_view():
 
     df = pd.DataFrame(data)
     ddf = dd.from_pandas(df, npartitions=2)
-    assert_eq(ddf["x"].view("uint8"), df["x"].view("uint8"))
-    assert_eq(ddf["y"].view("int64"), df["y"].view("int64"))
+
+    msg = "Will be removed in a future version. Use "
+    with pytest.raises(FutureWarning, match=msg):
+        assert_eq(ddf["x"].view("uint8"), df["x"].view("uint8"))
+        assert_eq(ddf["y"].view("int64"), df["y"].view("int64"))
 
 
 def test_simple_map_partitions():