@@ -1904,44 +1904,42 @@ def infer_config(args, constructor, trace_patch, layout_patch):
1904
1904
return trace_specs , grouped_mappings , sizeref , show_colorbar
1905
1905
1906
1906
1907
- def get_orderings (args , grouper ):
1907
+ def get_groups_and_orders (args , grouper ):
1908
1908
"""
1909
1909
`orders` is the user-supplied ordering with the remaining data-frame-supplied
1910
1910
ordering appended if the column is used for grouping. It includes anything the user
1911
1911
gave, for any variable, including values not present in the dataset. It's a dict
1912
1912
where the keys are e.g. "x" or "color"
1913
1913
1914
- `sorted_group_names ` is the set of groups, ordered by the order above. It's a list
1915
- of tuples like [("value1", ""), ("value2", "")] where each tuple contains the name
1914
+ `groups ` is the dicts of groups, ordered by the order above. Its keys are
1915
+ tuples like [("value1", ""), ("value2", "")] where each tuple contains the name
1916
1916
of a single dimension-group
1917
1917
"""
1918
1918
orders = {} if "category_orders" not in args else args ["category_orders" ].copy ()
1919
1919
1920
1920
# figure out orders and what the single group name would be if there were one
1921
1921
single_group_name = []
1922
+ unique_cache = dict ()
1922
1923
for col in grouper :
1923
1924
if col == one_group :
1924
1925
single_group_name .append ("" )
1925
1926
else :
1926
- uniques = list (args ["data_frame" ][col ].unique ())
1927
+ if col not in unique_cache :
1928
+ unique_cache [col ] = list (args ["data_frame" ][col ].unique ())
1929
+ uniques = unique_cache [col ]
1927
1930
if len (uniques ) == 1 :
1928
1931
single_group_name .append (uniques [0 ])
1929
1932
if col not in orders :
1930
1933
orders [col ] = uniques
1931
1934
else :
1932
1935
orders [col ] = list (OrderedDict .fromkeys (list (orders [col ]) + uniques ))
1933
-
1936
+ df = args [ "data_frame" ]
1934
1937
if len (single_group_name ) == len (grouper ):
1935
1938
# we have a single group, so we can skip all group-by operations!
1936
- grouped = None
1937
- sorted_group_names = [tuple (single_group_name )]
1939
+ groups = {tuple (single_group_name ): df }
1938
1940
else :
1939
- grouped = args ["data_frame" ].groupby (grouper , sort = False )
1940
- sorted_group_names = []
1941
- for group_name in grouped .groups :
1942
- if len (grouper ) == 1 :
1943
- group_name = (group_name ,)
1944
- sorted_group_names .append (group_name )
1941
+ group_indices = df .groupby (grouper , sort = False ).indices
1942
+ sorted_group_names = [g if len (grouper ) != 1 else (g ,) for g in group_indices ]
1945
1943
1946
1944
for i , col in reversed (list (enumerate (grouper ))):
1947
1945
if col != one_group :
@@ -1951,7 +1949,9 @@ def get_orderings(args, grouper):
1951
1949
if g [i ] in orders [col ]
1952
1950
else - 1 ,
1953
1951
)
1954
- return grouped , orders , sorted_group_names
1952
+
1953
+ groups = {s : df .iloc [group_indices [s ]] for s in sorted_group_names }
1954
+ return groups , orders
1955
1955
1956
1956
1957
1957
def make_figure (args , constructor , trace_patch = None , layout_patch = None ):
@@ -1970,7 +1970,7 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
1970
1970
args , constructor , trace_patch , layout_patch
1971
1971
)
1972
1972
grouper = [x .grouper or one_group for x in grouped_mappings ] or [one_group ]
1973
- grouped , orders , sorted_group_names = get_orderings (args , grouper )
1973
+ groups , orders = get_groups_and_orders (args , grouper )
1974
1974
1975
1975
col_labels = []
1976
1976
row_labels = []
@@ -1999,13 +1999,7 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
1999
1999
trendline_rows = []
2000
2000
trace_name_labels = None
2001
2001
facet_col_wrap = args .get ("facet_col_wrap" , 0 )
2002
- for group_name in sorted_group_names :
2003
- if grouped is not None :
2004
- group = grouped .get_group (
2005
- group_name if len (group_name ) > 1 else group_name [0 ]
2006
- )
2007
- else :
2008
- group = args ["data_frame" ]
2002
+ for group_name , group in groups .items ():
2009
2003
mapping_labels = OrderedDict ()
2010
2004
trace_name_labels = OrderedDict ()
2011
2005
frame_name = ""
0 commit comments