Skip to content

Commit

Permalink
exp.print_ can handle series with units
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-brajer committed Jun 5, 2021
1 parent aa88242 commit a12a068
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions physicslab/experiment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,25 @@ def process(data_list, by_module, **kwargs):


def print_(df):
""" Print the dataframe, include units row if available.
""" Print the data including units row if available.
Does not change the input DataFrame.
| Does not change the input DataFrame.
| If :class:`~pandas.Series` is supplied, it's printed in the
:class:`~pandas.DataFrame` format.
:param df:
:type df: pandas.DataFrame
: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.

df = pd.concat([df_units, df], axis=0) # Should do hard copy.

print(df)

0 comments on commit a12a068

Please sign in to comment.