<s> from __future__ import ( absolute_import , division , print_function , <EOL> unicode_literals ) <EOL> from collections import defaultdict <EOL> import pandas as pd <EOL> import numpy as np <EOL> from ggplot . utils import make_iterable_ntimes <EOL> from . stat import stat <EOL> _MSG_STATUS = """<STR_LIT>""" <EOL> class stat_bin2d ( stat ) : <EOL> REQUIRED_AES = { '<STR_LIT:x>' , '<STR_LIT:y>' } <EOL> DEFAULT_PARAMS = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:30> , '<STR_LIT>' : True , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:right>' : False } <EOL> CREATES = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> def _calculate ( self , data ) : <EOL> self . _print_warning ( _MSG_STATUS ) <EOL> x = data . pop ( '<STR_LIT:x>' ) <EOL> y = data . pop ( '<STR_LIT:y>' ) <EOL> bins = self . params [ '<STR_LIT>' ] <EOL> drop = self . params [ '<STR_LIT>' ] <EOL> right = self . params [ '<STR_LIT:right>' ] <EOL> weight = make_iterable_ntimes ( self . params [ '<STR_LIT>' ] , len ( x ) ) <EOL> x_assignments , xbreaks = pd . cut ( x , bins = bins , labels = False , <EOL> right = right , retbins = True ) <EOL> y_assignments , ybreaks = pd . cut ( y , bins = bins , labels = False , <EOL> right = right , retbins = True ) <EOL> df = pd . DataFrame ( { '<STR_LIT>' : x_assignments , <EOL> '<STR_LIT>' : y_assignments , <EOL> '<STR_LIT>' : weight } ) <EOL> table = pd . pivot_table ( df , values = '<STR_LIT>' , <EOL> index = [ '<STR_LIT>' , '<STR_LIT>' ] , aggfunc = np . sum ) <EOL> rects = np . array ( [ [ xbreaks [ i ] , xbreaks [ i + <NUM_LIT:1> ] , <EOL> ybreaks [ j ] , ybreaks [ j + <NUM_LIT:1> ] , <EOL> table [ ( i , j ) ] ] <EOL> for ( i , j ) in table . keys ( ) ] ) <EOL> new_data = pd . DataFrame ( rects , columns = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> new_data [ '<STR_LIT>' ] = [ '<STR_LIT>' ] * len ( new_data ) <EOL> n = len ( new_data ) <EOL> for ae in data : <EOL> new_data [ ae ] = make_iterable_ntimes ( data [ ae ] . iloc [ <NUM_LIT:0> ] , n ) <EOL> return new_data </s>
