@@ -970,27 +970,24 @@ def _isinstance_listlike(x):
970
970
return True
971
971
972
972
973
- def process_args_into_dataframe (args , wide_mode , var_name ):
973
+ def _escape_col_name (df_input , col_name ):
974
+ while df_input is not None and col_name in df_input .columns :
975
+ col_name = "_" + col_name
976
+ return col_name
977
+
978
+
979
+ def process_args_into_dataframe (args , wide_mode , var_name , value_name ):
974
980
"""
975
981
After this function runs, the `all_attrables` keys of `args` all contain only
976
982
references to columns of `df_output`. This function handles the extraction of data
977
983
from `args["attrable"]` and column-name-generation as appropriate, and adds the
978
984
data to `df_output` and then replaces `args["attrable"]` with the appropriate
979
985
reference.
980
986
"""
981
- for field in args :
982
- if field in array_attrables and args [field ] is not None :
983
- args [field ] = (
984
- OrderedDict (args [field ])
985
- if isinstance (args [field ], dict )
986
- else list (args [field ])
987
- )
988
- # Cast data_frame argument to DataFrame (it could be a numpy array, dict etc.)
989
- df_provided = args ["data_frame" ] is not None
990
- if df_provided and not isinstance (args ["data_frame" ], pd .DataFrame ):
991
- args ["data_frame" ] = pd .DataFrame (args ["data_frame" ])
987
+
992
988
df_input = args ["data_frame" ]
993
989
df_provided = df_input is not None
990
+
994
991
df_output = pd .DataFrame ()
995
992
constants = dict ()
996
993
ranges = list ()
@@ -1083,7 +1080,7 @@ def process_args_into_dataframe(args, wide_mode, var_name):
1083
1080
)
1084
1081
# Check validity of column name
1085
1082
if argument not in df_input .columns :
1086
- if wide_mode and argument in ("value" , var_name ):
1083
+ if wide_mode and argument in (value_name , var_name ):
1087
1084
continue
1088
1085
else :
1089
1086
err_msg = (
@@ -1205,10 +1202,11 @@ def build_dataframe(args, constructor):
1205
1202
wide_y = False if no_y else _is_col_list (df_input , args ["y" ])
1206
1203
1207
1204
wide_mode = False
1208
- var_name = None
1205
+ var_name = None # will likely be "variable" in wide_mode
1206
+ wide_cross_name = None # will likely be "index" in wide_mode
1207
+ value_name = "value"
1209
1208
hist2d_types = [go .Histogram2d , go .Histogram2dContour ]
1210
1209
if constructor in cartesians :
1211
- wide_cross_name = None
1212
1210
if wide_x and wide_y :
1213
1211
raise ValueError (
1214
1212
"Cannot accept list of column references or list of columns for both `x` and `y`."
@@ -1266,26 +1264,33 @@ def build_dataframe(args, constructor):
1266
1264
args ["wide_cross" ] = df_input .index
1267
1265
wide_cross_name = df_input .index .name or "index"
1268
1266
else :
1269
- args ["wide_cross" ] = Range (label = "index" )
1270
- wide_cross_name = "index"
1267
+ wide_cross_name = _escape_col_name (df_input , "index" )
1268
+ args ["wide_cross" ] = Range (label = wide_cross_name )
1269
+
1270
+ if wide_mode :
1271
+ var_name = _escape_col_name (df_input , var_name )
1272
+ value_name = _escape_col_name (df_input , value_name )
1271
1273
1272
1274
# now that things have been prepped, we do the systematic rewriting of `args`
1273
1275
1274
- df_output , wide_id_vars = process_args_into_dataframe (args , wide_mode , var_name )
1276
+ df_output , wide_id_vars = process_args_into_dataframe (
1277
+ args , wide_mode , var_name , value_name
1278
+ )
1275
1279
1276
1280
# now that `df_output` exists and `args` contains only references, we complete
1277
1281
# the special-case and wide-mode handling by further rewriting args and/or mutating
1278
1282
# df_output
1279
1283
1284
+ count_name = _escape_col_name (df_output , "count" )
1280
1285
if not wide_mode and missing_bar_dim and constructor == go .Bar :
1281
1286
# now that we've populated df_output, we check to see if the non-missing
1282
1287
# dimension is categorical: if so, then setting the missing dimension to a
1283
1288
# constant 1 is a less-insane thing to do than setting it to the index by
1284
1289
# default and we let the normal auto-orientation-code do its thing later
1285
1290
other_dim = "x" if missing_bar_dim == "y" else "y"
1286
1291
if not _is_continuous (df_output , args [other_dim ]):
1287
- args [missing_bar_dim ] = "count"
1288
- df_output ["count" ] = 1
1292
+ args [missing_bar_dim ] = count_name
1293
+ df_output [count_name ] = 1
1289
1294
else :
1290
1295
# on the other hand, if the non-missing dimension is continuous, then we
1291
1296
# can use this information to override the normal auto-orientation code
@@ -1306,7 +1311,7 @@ def build_dataframe(args, constructor):
1306
1311
id_vars = wide_id_vars ,
1307
1312
value_vars = wide_value_vars ,
1308
1313
var_name = var_name ,
1309
- value_name = "value" ,
1314
+ value_name = value_name ,
1310
1315
)
1311
1316
df_output [var_name ] = df_output [var_name ].astype (str )
1312
1317
orient_v = wide_orientation == "v"
@@ -1317,24 +1322,24 @@ def build_dataframe(args, constructor):
1317
1322
1318
1323
if constructor in [go .Scatter , go .Funnel ] + hist2d_types :
1319
1324
args ["x" if orient_v else "y" ] = wide_cross_name
1320
- args ["y" if orient_v else "x" ] = "value"
1325
+ args ["y" if orient_v else "x" ] = value_name
1321
1326
if constructor != go .Histogram2d :
1322
1327
args ["color" ] = args ["color" ] or var_name
1323
1328
if constructor == go .Bar :
1324
- if _is_continuous (df_output , "value" ):
1329
+ if _is_continuous (df_output , value_name ):
1325
1330
args ["x" if orient_v else "y" ] = wide_cross_name
1326
- args ["y" if orient_v else "x" ] = "value"
1331
+ args ["y" if orient_v else "x" ] = value_name
1327
1332
args ["color" ] = args ["color" ] or var_name
1328
1333
else :
1329
- args ["x" if orient_v else "y" ] = "value"
1330
- args ["y" if orient_v else "x" ] = "count"
1331
- df_output ["count" ] = 1
1334
+ args ["x" if orient_v else "y" ] = value_name
1335
+ args ["y" if orient_v else "x" ] = count_name
1336
+ df_output [count_name ] = 1
1332
1337
args ["color" ] = args ["color" ] or var_name
1333
1338
if constructor in [go .Violin , go .Box ]:
1334
1339
args ["x" if orient_v else "y" ] = wide_cross_name or var_name
1335
- args ["y" if orient_v else "x" ] = "value"
1340
+ args ["y" if orient_v else "x" ] = value_name
1336
1341
if constructor == go .Histogram :
1337
- args ["x" if orient_v else "y" ] = "value"
1342
+ args ["x" if orient_v else "y" ] = value_name
1338
1343
args ["y" if orient_v else "x" ] = wide_cross_name
1339
1344
args ["color" ] = args ["color" ] or var_name
1340
1345
0 commit comments