@@ -196,36 +196,48 @@ def save_data(obj, fname, type=None, apply_suffix: bool = True) -> "str | Sequen
196196
197197def default_title (obj , name ):
198198 try :
199- _choose_df_lib (obj )
199+ df_lib = _choose_df_lib (obj )
200200 except NotImplementedError :
201201 obj_name = type (obj ).__qualname__
202202 return f"{ name } : a pinned { obj_name } object"
203203
204+ _df_lib_to_objname : dict [_DFLib , str ] = {
205+ "polars" : "DataFrame" ,
206+ "pandas" : "DataFrame" ,
207+ }
208+
204209 # TODO(compat): title says CSV rather than data.frame
205210 # see https://github.com/machow/pins-python/issues/5
206211 shape_str = " x " .join (map (str , obj .shape ))
207- return f"{ name } : a pinned { shape_str } DataFrame "
212+ return f"{ name } : a pinned { shape_str } { _df_lib_to_objname [ df_lib ] } "
208213
209214
210215def _choose_df_lib (
211216 df ,
212217 * ,
213- supported_libs : list [_DFLib ] = [ "pandas" , "polars" ] ,
218+ supported_libs : list [_DFLib ] | None = None ,
214219 file_type : str | None = None ,
215220) -> _DFLib :
216- """Return the type of DataFrame library used in the given DataFrame.
221+ """Return the library associated with a DataFrame, e.g. "pandas".
222+
223+ The arguments `supported_libs` and `file_type` must be specified together, and are
224+ meant to be used when saving an object, to choose the appropriate library.
217225
218226 Args:
219227 df:
220228 The object to check - might not be a DataFrame necessarily.
221229 supported_libs:
222230 The DataFrame libraries to accept for this df.
223231 file_type:
224- The file type we're trying to save to - used to give more specific error messages.
232+ The file type we're trying to save to - used to give more specific error
233+ messages.
225234
226235 Raises:
227- NotImplementedError: If the DataFrame type is not recognized.
236+ NotImplementedError: If the DataFrame type is not recognized, or not supported .
228237 """
238+ if (supported_libs is None ) + (file_type is None ) == 1 :
239+ raise ValueError ("Must provide both or neither of supported_libs and file_type" )
240+
229241 df_libs : list [_DFLib ] = []
230242
231243 # pandas
@@ -243,6 +255,7 @@ def _choose_df_lib(
243255 if isinstance (df , pl .DataFrame ):
244256 df_libs .append ("polars" )
245257
258+ # Make sure there's only one library associated with the dataframe
246259 if len (df_libs ) == 1 :
247260 (df_lib ,) = df_libs
248261 elif len (df_libs ) > 1 :
@@ -255,16 +268,14 @@ def _choose_df_lib(
255268 else :
256269 raise NotImplementedError (f"Unrecognized DataFrame type: { type (df )} " )
257270
258- if df_lib not in supported_libs :
259- if file_type is None :
260- ftype_clause = "in pins"
261- else :
262- ftype_clause = f"for type { file_type !r} "
271+ # Raise if the library is not supported
272+ if supported_libs is not None and df_lib not in supported_libs :
273+ ftype_clause = f"for type { file_type !r} "
263274
264275 if len (supported_libs ) == 1 :
265276 msg = (
266277 f"Currently only { supported_libs [0 ]} DataFrames can be saved "
267- f"{ ftype_clause } . { df_lib } DataFrames are not yet supported."
278+ f"{ ftype_clause } . DataFrames from { df_lib } are not yet supported."
268279 )
269280 else :
270281 msg = (
0 commit comments