@@ -197,36 +197,48 @@ def save_data(obj, fname, type=None, apply_suffix: bool = True) -> "str | Sequen
197197
198198def default_title (obj , name ):
199199 try :
200- _choose_df_lib (obj )
200+ df_lib = _choose_df_lib (obj )
201201 except NotImplementedError :
202202 obj_name = type (obj ).__qualname__
203203 return f"{ name } : a pinned { obj_name } object"
204204
205+ _df_lib_to_objname : dict [_DFLib , str ] = {
206+ "polars" : "DataFrame" ,
207+ "pandas" : "DataFrame" ,
208+ }
209+
205210 # TODO(compat): title says CSV rather than data.frame
206211 # see https://github.com/machow/pins-python/issues/5
207212 shape_str = " x " .join (map (str , obj .shape ))
208- return f"{ name } : a pinned { shape_str } DataFrame "
213+ return f"{ name } : a pinned { shape_str } { _df_lib_to_objname [ df_lib ] } "
209214
210215
211216def _choose_df_lib (
212217 df ,
213218 * ,
214- supported_libs : list [_DFLib ] = [ "pandas" , "polars" ] ,
219+ supported_libs : list [_DFLib ] | None = None ,
215220 file_type : str | None = None ,
216221) -> _DFLib :
217- """Return the type of DataFrame library used in the given DataFrame.
222+ """Return the library associated with a DataFrame, e.g. "pandas".
223+
224+ The arguments `supported_libs` and `file_type` must be specified together, and are
225+ meant to be used when saving an object, to choose the appropriate library.
218226
219227 Args:
220228 df:
221229 The object to check - might not be a DataFrame necessarily.
222230 supported_libs:
223231 The DataFrame libraries to accept for this df.
224232 file_type:
225- The file type we're trying to save to - used to give more specific error messages.
233+ The file type we're trying to save to - used to give more specific error
234+ messages.
226235
227236 Raises:
228- NotImplementedError: If the DataFrame type is not recognized.
237+ NotImplementedError: If the DataFrame type is not recognized, or not supported .
229238 """
239+ if (supported_libs is None ) + (file_type is None ) == 1 :
240+ raise ValueError ("Must provide both or neither of supported_libs and file_type" )
241+
230242 df_libs : list [_DFLib ] = []
231243
232244 # pandas
@@ -244,6 +256,7 @@ def _choose_df_lib(
244256 if isinstance (df , pl .DataFrame ):
245257 df_libs .append ("polars" )
246258
259+ # Make sure there's only one library associated with the dataframe
247260 if len (df_libs ) == 1 :
248261 (df_lib ,) = df_libs
249262 elif len (df_libs ) > 1 :
@@ -256,16 +269,14 @@ def _choose_df_lib(
256269 else :
257270 raise NotImplementedError (f"Unrecognized DataFrame type: { type (df )} " )
258271
259- if df_lib not in supported_libs :
260- if file_type is None :
261- ftype_clause = "in pins"
262- else :
263- ftype_clause = f"for type { file_type !r} "
272+ # Raise if the library is not supported
273+ if supported_libs is not None and df_lib not in supported_libs :
274+ ftype_clause = f"for type { file_type !r} "
264275
265276 if len (supported_libs ) == 1 :
266277 msg = (
267278 f"Currently only { supported_libs [0 ]} DataFrames can be saved "
268- f"{ ftype_clause } . { df_lib } DataFrames are not yet supported."
279+ f"{ ftype_clause } . DataFrames from { df_lib } are not yet supported."
269280 )
270281 else :
271282 msg = (
0 commit comments