88import re
99import sys
1010import warnings
11+ import narwhals .stable .v1 as nw
1112
1213from _plotly_utils .optional_imports import get_module
1314
@@ -72,8 +73,6 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False):
7273 """
7374 np = get_module ("numpy" )
7475
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 )
7776 assert np is not None
7877
7978 # ### Process kind ###
@@ -93,34 +92,26 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False):
9392 "O" : "object" ,
9493 }
9594
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 ()
124115
125116 if not isinstance (v , np .ndarray ):
126117 # v has its own logic on how to convert itself into a numpy array
@@ -193,6 +184,7 @@ def is_homogeneous_array(v):
193184 np
194185 and isinstance (v , np .ndarray )
195186 or (pd and isinstance (v , (pd .Series , pd .Index )))
187+ or (isinstance (v , nw .Series ))
196188 ):
197189 return True
198190 if is_numpy_convertable (v ):
0 commit comments