def main(args=None, print_func=None): """Entry point for command line :args - list of str, as in sys.argv :print_func - optional, callable. If provided, output is passed to this function instead of print(). Used for testing. """ if not args: args = sys.argv if print_func: print=print_func else: print=builtins.print ### usage this = os.path.basename(args.pop(0)) cl_hint='' if this=='__main__.py': this='python -m dfx' cl_hint="\n\nThis can also be called with 'dfx [...]'" usage="""Command line entry for dfx > {this:} my_data.csv # show column types on dataset > {this:} my_data.csv my_col # show detail on that column > {this:} my_data.csv my_col norm # show num_normal pattern for that column > {this:} my_data.csv my_col norm source # show class/file for rule > {this:} --test # run tests > {this:} # this screen{hint:}""".format( this=this, hint=cl_hint) if not args or any([_ in args for _ in ['-h', '-?', 'help']]): print(usage) return ### run tests if args[0] == '--test': args.pop(0) test_dir = os.path.join( os.path.dirname(os.path.dirname(__file__)), 'tests') test_file = os.path.join( test_dir, 'test_ops.py') sys.path.append(test_dir) import unittest_dfx import unittest unittest.main(test_file) return unittest_dfx.main(test_file) return
df = datasets.regions g = dfx.GrainDf(df, columns=['state', 'county', 'town'], force=True) act_rows = g.town.filter_up('penfield') # get expected rows rows = df[df.town == 'penfield'] ids = rows.groupby(['state', 'county']).size().index indexed_df = df.set_index(['state', 'county']) exp_rows = indexed_df.loc[ids].reset_index() self.assertEqualDf(act_rows, exp_rows) def test_filter_up_hier_also_above(self): """Filter up but ignore this additional field """ df = datasets.regions g = dfx.GrainDf(df, columns=['state', 'county', 'town'], force=True) act_rows = g.town.filter_up('penfield', also_above='county') # get expected rows rows = df[df.town == 'penfield'] ids = rows.groupby(['state']).size().index indexed_df = df.set_index(['state', 'county']) exp_rows = indexed_df.loc[ids].reset_index() self.assertEqualDf(act_rows, exp_rows) if __name__ == '__main__': sys.exit(unittest_dfx.main(__file__))