read_csv()

audformat.utils.read_csv(*args, as_dataframe=False, **kwargs)[source]

Read object from CSV file.

Automatically detects the index type and returns an object that is conform to table specifications. If conversion is not possible, an error is raised.

Time values for the start and end column are converted using pandas.to_timedelta(), whereby float and integers are treated as seconds.

Parameters
  • *args – arguments passed on to pandas.read_csv()

  • as_dataframe (bool) – if False, a dataframe is only returned for data with two or more columns, a series for data with one column, an index for data with zero columns

  • **kwargs – keyword arguments passed on to pandas.read_csv()

Return type

Union[Index, Series, DataFrame]

Returns

object conform to table specifications

Raises

ValueError – if CSV file is not conform to table specifications

Examples

>>> string = '''file,start,end,value
... f1,00:00:00,00:00:01,0.0
... f1,00:00:01,00:00:02,1.0
... f2,00:00:02,00:00:03,2.0'''
>>> with open("file.csv", "w") as file:
...     _ = file.write(string)
>>> read_csv("file.csv")
file  start            end
f1    0 days 00:00:00  0 days 00:00:01    0.0
      0 days 00:00:01  0 days 00:00:02    1.0
f2    0 days 00:00:02  0 days 00:00:03    2.0
Name: value, dtype: float64
>>> string = '''file,start,end,value
... f1,0,1,0.0
... f1,1,2,1.0
... f2,2,3,2.0'''
>>> with open("file.csv", "w") as file:
...     _ = file.write(string)
>>> read_csv("file.csv")
file  start            end
f1    0 days 00:00:00  0 days 00:00:01    0.0
      0 days 00:00:01  0 days 00:00:02    1.0
f2    0 days 00:00:02  0 days 00:00:03    2.0
Name: value, dtype: float64
>>> read_csv("file.csv", as_dataframe=True)
                                      value
file start           end
f1   0 days 00:00:00 0 days 00:00:01    0.0
     0 days 00:00:01 0 days 00:00:02    1.0
f2   0 days 00:00:02 0 days 00:00:03    2.0