browse

Helpers for browse, head, and tail magics

Parsing Stata code for browse magic


source

in_range

 in_range (stata_in_code, count)

Return in-statement range

test_eq(in_range("", 100), (None, None))
test_eq(in_range("in 10", 100), (0, 10))
test_eq(in_range("in 1/10", 100), (0, 10))
test_eq(in_range("in F/10", 100), (0, 10))
test_eq(in_range("in 3/10", 100), (2, 10))
test_fail(in_range, args=("in 10", 5))
test_fail(in_range, args=("in 10/1", 100))

Negative indices are converted to positive, following Stata syntax:

test_eq(in_range("in -10/-1", 41), (31, 41))

Head/tail/browse magic helpers


source

parse_browse_magic

 parse_browse_magic (code)
code = "le if year==1920 in 1/10, noformat"
test_eq(parse_browse_magic(code), 
        (None, ['le'], 'if year==1920', 'in 1/10', '', 'noformat')
       )
run_single("gen date = dofy(year)")
test_eq(parse_browse_magic("10 le* if date==mdy(1, 1, 1910), noformat"),
        (10, ['le'], 'if date==mdy(1, 1, 1910)', '', '', 'noformat')
       )
run_single("drop date")

source

get_df

 get_df (obs_range, var, stata_if_code, missingval, valuelabel, sformat)

Head/tail magic helpers


source

headtail_df_params

 headtail_df_params (code, count, missing_config, tail=False)
test_eq(headtail_df_params("", 50, "."),
        ((range(0, 5), None, '', ".", True, True)))
test_eq(headtail_df_params("10 y le*, noformat", 50, "."),
        ((range(0, 10), ['year', 'le'], '', ".", True, False)))
test_eq(headtail_df_params("10 y le*, noformat", 50, ".", tail=True),
        ((range(40, 50), ['year', 'le'], '', ".", True, False)))

source

headtail_get_df

 headtail_get_df (obs_range, var, stata_if_code, missingval, valuelabel,
                  sformat)
headtail_get_df(*headtail_df_params('if year<1903', obs_count(), "."))
year le
1 1900 47.3
2 1901 .
3 1902 51.5
test_eq(headtail_get_df(*headtail_df_params('if year<1903', obs_count(), ".")),
        headtail_get_df(*headtail_df_params('if year<1903', obs_count(), ".", tail=True)),)
headtail_get_df(*headtail_df_params('if year>1910', obs_count(), "."))
year le
12 1911 52.6
13 1912 53.5
14 1913 52.5
15 1914 54.2
16 1915 54.5

Browse magic helpers


source

browse_df_params

 browse_df_params (code, count, missing_config)
browse_df_params("", 10, 'pandas')
(None, None, '', nan, True, True)
browse_df_params("y le* if year<1910, noformat", 50, '.')
(None, ['year', 'le'], 'if year<1910', '.', True, False)
browse_df_params('in 1/5', 41, '.')
(range(0, 5), None, '', '.', True, True)
get_df(*browse_df_params('in 1/5', obs_count(), '.'))
year le
1 1900 47.3
2 1901 .
3 1902 51.5
4 1903 50.5
5 1904 47.6

source

set_ipydatagrid_height

 set_ipydatagrid_height ()

source

display_df_as_ipydatagrid

 display_df_as_ipydatagrid (df, auto_height=True)
#set_ipydatagrid_height()
run_single('gen long_name_1234567 = ""')
run_single('gen long_str = "10000000000200000000003000000000040000000000500000000006000000000070000000000800000000009000000000010000000000"')
(41 missing values generated)
df = get_df(*browse_df_params(', noformat', obs_count(), '.'))
display_df_as_ipydatagrid(df)
#set_ipydatagrid_height()
run_single('sysuse auto, clear')
(1978 automobile data)
df = get_df(*browse_df_params('', obs_count(), '.'))
display_df_as_ipydatagrid(df, False)