Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds robustness to create_dataframe() and couple of other places #211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions acro/acro_stata_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def parse_table_details(
>> table rowvar [colvar [supercolvar] [if] [in] [weight] [, options].
"""
details: dict = {"errmsg": "", "rowvars": list([]), "colvars": list([])}
contents_found, content = False, []

if stata_version == "16":
details["rowvars"] = [varlist.pop(0)]
Expand Down Expand Up @@ -326,6 +327,7 @@ def extract_var_within_parentheses(input_string):
"""Given a string, this function extracts the words within the first parentheses
from a string.
"""
string = ""
string_match = re.match(r"\((.*?)\)", input_string)
if string_match:
string = string_match.group(1).strip()
Expand All @@ -335,6 +337,7 @@ def extract_var_within_parentheses(input_string):

def extract_var_before_parentheses(input_string):
"""Given a string, this function extracts the words before the first parentheses."""
string = ""
string_match = re.match(r"^(.*?)\(", input_string)
if string_match:
string = string_match.group(1).strip()
Expand All @@ -346,6 +349,7 @@ def extract_table_var(input_string):
"""Given a string, this function extracts the words within the parentheses.
If there are no parentheses the string is returned.
"""
string = ""
# If the string starts with parentheses
if input_string.startswith("("):
string, _ = extract_var_within_parentheses(input_string)
Expand Down
50 changes: 33 additions & 17 deletions acro/acro_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ def hist( # pylint: disable=too-many-arguments,too-many-locals
self,
data,
column,
by=None,
by_val=None,
grid=True,
xlabelsize=None,
xrot=None,
ylabelsize=None,
yrot=None,
ax=None,
axis=None,
sharex=False,
sharey=False,
figsize=None,
Expand All @@ -586,7 +586,7 @@ def hist( # pylint: disable=too-many-arguments,too-many-locals
The pandas object holding the data.
column : str
The column that will be used to plot the histogram.
by : object, optional
by_val : object, optional
If passed, then used to form histograms for separate groups.
grid : bool, default True
Whether to show axis grid lines.
Expand All @@ -600,7 +600,7 @@ def hist( # pylint: disable=too-many-arguments,too-many-locals
yrot : float, default None
Rotation of y axis labels. For example, a value of 90 displays
the y labels rotated 90 degrees clockwise.
ax : Matplotlib axes object, default None
axis : Matplotlib axes object, default None
The axes to plot the histogram on.
sharex : bool, default True if ax is None else False
In case subplots=True, share x axis and set some x axis labels to invisible;
Expand Down Expand Up @@ -662,13 +662,13 @@ def hist( # pylint: disable=too-many-arguments,too-many-locals
else: # pragma: no cover
data.hist(
column=column,
by=by,
by=by_val,
grid=grid,
xlabelsize=xlabelsize,
xrot=xrot,
ylabelsize=ylabelsize,
yrot=yrot,
ax=ax,
ax=axis,
sharex=sharex,
sharey=sharey,
figsize=figsize,
Expand All @@ -682,13 +682,13 @@ def hist( # pylint: disable=too-many-arguments,too-many-locals
status = "review"
data.hist(
column=column,
by=by,
by=by_val,
grid=grid,
xlabelsize=xlabelsize,
xrot=xrot,
ylabelsize=ylabelsize,
yrot=yrot,
ax=ax,
ax=axis,
sharex=sharex,
sharey=sharey,
figsize=figsize,
Expand Down Expand Up @@ -1364,15 +1364,31 @@ def create_dataframe(index, columns) -> DataFrame:
Dataframe
Table of the index and columns combined.
"""
if isinstance(index, list):
index_df = pd.concat(index, axis=1)
elif isinstance(index, pd.Series):
index_df = pd.DataFrame({index.name: index})
if isinstance(columns, list):
columns_df = pd.concat(columns, axis=1)
elif isinstance(columns, pd.Series):
columns_df = pd.DataFrame({columns.name: columns})
data = pd.concat([index_df, columns_df], axis=1)
empty_dataframe = pd.DataFrame([])

index_df = empty_dataframe
try:
if isinstance(index, list):
index_df = pd.concat(index, axis=1)
elif isinstance(index, pd.Series):
index_df = pd.DataFrame({index.name: index})
except ValueError:
index_df = empty_dataframe

columns_df = empty_dataframe
try:
if isinstance(columns, list):
columns_df = pd.concat(columns, axis=1)
elif isinstance(columns, pd.Series):
columns_df = pd.DataFrame({columns.name: columns})
except ValueError:
columns_df = empty_dataframe

try:
data = pd.concat([index_df, columns_df], axis=1)
except ValueError:
data = empty_dataframe

return data


Expand Down
Loading