Skip to content

Commit

Permalink
root print refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-brajer committed Feb 26, 2022
1 parent d99db0e commit 5aebdb4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions physicslab/experiment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def process(data_list: list, by_module: ModuleType, **kwargs) -> pd.DataFrame:
return df


def print_(df: Union[pd.DataFrame, pd.Series]) -> None:
def print(df: Union[pd.DataFrame, pd.Series]) -> None:
""" Print the data including units row if available.
| Does not change the input DataFrame.
Expand All @@ -58,16 +58,18 @@ def print_(df: Union[pd.DataFrame, pd.Series]) -> None:
:param df: Data to be printed
:type df: pandas.DataFrame or pandas.Series
"""
if isinstance(df, pd.Series):
df_new = pd.DataFrame(df).transpose()
if df.attrs and UNITS in df.attrs:
df_new.attrs[UNITS] = df.attrs[UNITS]
df = df_new

# :attr:`pandas.Dataframe.attrs` is experimental. See:
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.attrs.html
if df.attrs and UNITS in df.attrs:
df_units = pd.DataFrame(df.attrs[UNITS]).transpose() # Make it a row.
units = df.attrs[UNITS]
else:
units = None

if isinstance(df, pd.Series): # Must be after units readout.
df = pd.DataFrame(df).transpose()

if units is not None:
df_units = pd.DataFrame(units).transpose() # Make it a row.
df = pd.concat([df_units, df], axis=0) # Should do hard copy.

print(df)

0 comments on commit 5aebdb4

Please sign in to comment.