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