text stringlengths 0 828 |
|---|
one_col_df = pd.read_csv(file_path, encoding=encoding, **kwargs) |
if one_col_df.shape[1] == 1: |
return one_col_df[one_col_df.columns[0]] |
else: |
raise Exception('Cannot build a series from this csv: it has more than two columns (one index + one value).' |
' Probably the parsing chain $read_df_or_series_from_csv => single_row_or_col_df_to_series$' |
'will work, though.') |
else: |
return pd.read_csv(file_path, encoding=encoding, **kwargs)" |
1533,"def get_default_pandas_parsers() -> List[AnyParser]: |
"""""" |
Utility method to return the default parsers able to parse a dictionary from a file. |
:return: |
"""""" |
return [SingleFileParserFunction(parser_function=read_dataframe_from_xls, |
streaming_mode=False, |
supported_exts={'.xls', '.xlsx', '.xlsm'}, |
supported_types={pd.DataFrame}, |
option_hints=pandas_parsers_option_hints_xls), |
SingleFileParserFunction(parser_function=read_df_or_series_from_csv, |
streaming_mode=False, |
supported_exts={'.csv', '.txt'}, |
supported_types={pd.DataFrame, pd.Series}, |
option_hints=pandas_parsers_option_hints_csv), |
]" |
1534,"def dict_to_df(desired_type: Type[T], dict_obj: Dict, logger: Logger, orient: str = None, **kwargs) -> pd.DataFrame: |
"""""" |
Helper method to convert a dictionary into a dataframe. It supports both simple key-value dicts as well as true |
table dicts. For this it uses pd.DataFrame constructor or pd.DataFrame.from_dict intelligently depending on the |
case. |
The orientation of the resulting dataframe can be configured, or left to default behaviour. Default orientation is |
different depending on the contents: |
* 'index' for 2-level dictionaries, in order to align as much as possible with the natural way to express rows in |
JSON |
* 'columns' for 1-level (simple key-value) dictionaries, so as to preserve the data types of the scalar values in |
the resulting dataframe columns if they are different |
:param desired_type: |
:param dict_obj: |
:param logger: |
:param orient: the orientation of the resulting dataframe. |
:param kwargs: |
:return: |
"""""" |
if len(dict_obj) > 0: |
first_val = dict_obj[next(iter(dict_obj))] |
if isinstance(first_val, dict) or isinstance(first_val, list): |
# --'full' table |
# default is index orientation |
orient = orient or 'index' |
# if orient is 'columns': |
# return pd.DataFrame(dict_obj) |
# else: |
return pd.DataFrame.from_dict(dict_obj, orient=orient) |
else: |
# --scalar > single-row or single-col |
# default is columns orientation |
orient = orient or 'columns' |
if orient is 'columns': |
return pd.DataFrame(dict_obj, index=[0]) |
else: |
res = pd.DataFrame.from_dict(dict_obj, orient=orient) |
res.index.name = 'key' |
return res.rename(columns={0: 'value'}) |
else: |
# for empty dictionaries, orientation does not matter |
# but maybe we should still create a column 'value' in this empty dataframe ? |
return pd.DataFrame.from_dict(dict_obj)" |
1535,"def single_row_or_col_df_to_series(desired_type: Type[T], single_rowcol_df: pd.DataFrame, logger: Logger, **kwargs)\ |
-> pd.Series: |
"""""" |
Helper method to convert a dataframe with one row or one or two columns into a Series |
:param desired_type: |
:param single_col_df: |
:param logger: |
:param kwargs: |
:return: |
"""""" |
if single_rowcol_df.shape[0] == 1: |
# one row |
return single_rowcol_df.transpose()[0] |
elif single_rowcol_df.shape[1] == 2 and isinstance(single_rowcol_df.index, pd.RangeIndex): |
# two columns but the index contains nothing but the row number : we can use the first column |
d = single_rowcol_df.set_index(single_rowcol_df.columns[0]) |
return d[d.columns[0]] |
elif single_rowcol_df.shape[1] == 1: |
# one column and one index |
d = single_rowcol_df |
return d[d.columns[0]] |
else: |
raise ValueError('Unable to convert provided dataframe to a series : ' |
'expected exactly 1 row or 1 column, found : ' + str(single_rowcol_df.shape) + '')" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.