8
8
import re
9
9
import sys
10
10
import warnings
11
+ import narwhals .stable .v1 as nw
11
12
12
13
from _plotly_utils .optional_imports import get_module
13
14
@@ -72,8 +73,6 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False):
72
73
"""
73
74
np = get_module ("numpy" )
74
75
75
- # Don't force pandas to be loaded, we only want to know if it's already loaded
76
- pd = get_module ("pandas" , should_load = False )
77
76
assert np is not None
78
77
79
78
# ### Process kind ###
@@ -93,34 +92,26 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False):
93
92
"O" : "object" ,
94
93
}
95
94
96
- # Handle pandas Series and Index objects
97
- if pd and isinstance (v , (pd .Series , pd .Index )):
98
- if v .dtype .kind in numeric_kinds :
99
- # Get the numeric numpy array so we use fast path below
100
- v = v .values
101
- elif v .dtype .kind == "M" :
102
- # Convert datetime Series/Index to numpy array of datetimes
103
- if isinstance (v , pd .Series ):
104
- with warnings .catch_warnings ():
105
- warnings .simplefilter ("ignore" , FutureWarning )
106
- # Series.dt.to_pydatetime will return Index[object]
107
- # https://github.com/pandas-dev/pandas/pull/52459
108
- v = np .array (v .dt .to_pydatetime ())
109
- else :
110
- # DatetimeIndex
111
- v = v .to_pydatetime ()
112
- elif pd and isinstance (v , pd .DataFrame ) and len (set (v .dtypes )) == 1 :
113
- dtype = v .dtypes .tolist ()[0 ]
114
- if dtype .kind in numeric_kinds :
115
- v = v .values
116
- elif dtype .kind == "M" :
117
- with warnings .catch_warnings ():
118
- warnings .simplefilter ("ignore" , FutureWarning )
119
- # Series.dt.to_pydatetime will return Index[object]
120
- # https://github.com/pandas-dev/pandas/pull/52459
121
- v = [
122
- np .array (row .dt .to_pydatetime ()).tolist () for i , row in v .iterrows ()
123
- ]
95
+ # With `pass_through=True`, the original object will be returned if unable to convert
96
+ # to a Narwhals DataFrame or Series.
97
+ v = nw .from_native (v , allow_series = True , pass_through = True )
98
+
99
+ if isinstance (v , nw .Series ):
100
+ if v .dtype == nw .Datetime and v .dtype .time_zone is not None :
101
+ # Remove time zone so that local time is displayed
102
+ v = v .dt .replace_time_zone (None ).to_numpy ()
103
+ else :
104
+ v = v .to_numpy ()
105
+ elif isinstance (v , nw .DataFrame ):
106
+ schema = v .schema
107
+ overrides = {}
108
+ for key , val in schema .items ():
109
+ if val == nw .Datetime and val .time_zone is not None :
110
+ # Remove time zone so that local time is displayed
111
+ overrides [key ] = nw .col (key ).dt .replace_time_zone (None )
112
+ if overrides :
113
+ v = v .with_columns (** overrides )
114
+ v = v .to_numpy ()
124
115
125
116
if not isinstance (v , np .ndarray ):
126
117
# v has its own logic on how to convert itself into a numpy array
@@ -193,6 +184,7 @@ def is_homogeneous_array(v):
193
184
np
194
185
and isinstance (v , np .ndarray )
195
186
or (pd and isinstance (v , (pd .Series , pd .Index )))
187
+ or (isinstance (v , nw .Series ))
196
188
):
197
189
return True
198
190
if is_numpy_convertable (v ):
0 commit comments