@@ -196,36 +196,48 @@ def save_data(obj, fname, type=None, apply_suffix: bool = True) -> "str | Sequen
196
196
197
197
def default_title (obj , name ):
198
198
try :
199
- _choose_df_lib (obj )
199
+ df_lib = _choose_df_lib (obj )
200
200
except NotImplementedError :
201
201
obj_name = type (obj ).__qualname__
202
202
return f"{ name } : a pinned { obj_name } object"
203
203
204
+ _df_lib_to_objname : dict [_DFLib , str ] = {
205
+ "polars" : "DataFrame" ,
206
+ "pandas" : "DataFrame" ,
207
+ }
208
+
204
209
# TODO(compat): title says CSV rather than data.frame
205
210
# see https://github.com/machow/pins-python/issues/5
206
211
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 ] } "
208
213
209
214
210
215
def _choose_df_lib (
211
216
df ,
212
217
* ,
213
- supported_libs : list [_DFLib ] = [ "pandas" , "polars" ] ,
218
+ supported_libs : list [_DFLib ] | None = None ,
214
219
file_type : str | None = None ,
215
220
) -> _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.
217
225
218
226
Args:
219
227
df:
220
228
The object to check - might not be a DataFrame necessarily.
221
229
supported_libs:
222
230
The DataFrame libraries to accept for this df.
223
231
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.
225
234
226
235
Raises:
227
- NotImplementedError: If the DataFrame type is not recognized.
236
+ NotImplementedError: If the DataFrame type is not recognized, or not supported .
228
237
"""
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
+
229
241
df_libs : list [_DFLib ] = []
230
242
231
243
# pandas
@@ -243,6 +255,7 @@ def _choose_df_lib(
243
255
if isinstance (df , pl .DataFrame ):
244
256
df_libs .append ("polars" )
245
257
258
+ # Make sure there's only one library associated with the dataframe
246
259
if len (df_libs ) == 1 :
247
260
(df_lib ,) = df_libs
248
261
elif len (df_libs ) > 1 :
@@ -255,16 +268,14 @@ def _choose_df_lib(
255
268
else :
256
269
raise NotImplementedError (f"Unrecognized DataFrame type: { type (df )} " )
257
270
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} "
263
274
264
275
if len (supported_libs ) == 1 :
265
276
msg = (
266
277
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."
268
279
)
269
280
else :
270
281
msg = (
0 commit comments