audformat 1.3.3__tar.gz → 1.3.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {audformat-1.3.3 → audformat-1.3.4}/CHANGELOG.rst +6 -0
- {audformat-1.3.3 → audformat-1.3.4}/PKG-INFO +1 -1
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/common.py +15 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/database.py +18 -7
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/index.py +8 -4
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/scheme.py +2 -9
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/table.py +9 -12
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/testing.py +3 -2
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/utils.py +63 -14
- {audformat-1.3.3 → audformat-1.3.4}/audformat.egg-info/PKG-INFO +1 -1
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-misc-tables.rst +1 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_column.py +3 -3
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_database_get.py +57 -4
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_index.py +58 -4
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_misc_table.py +52 -61
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_scheme.py +89 -3
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_table.py +3 -2
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_utils.py +572 -23
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_utils_concat.py +5 -2
- {audformat-1.3.3 → audformat-1.3.4}/.github/workflows/doc.yml +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/.github/workflows/linter.yml +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/.github/workflows/publish.yml +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/.github/workflows/test.yml +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/.gitignore +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/.pre-commit-config.yaml +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/CONTRIBUTING.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/LICENSE +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/README.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/__init__.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/__init__.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/attachment.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/column.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/conftest.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/define.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/errors.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/media.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/rater.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/split.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/core/typing.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/define/__init__.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/errors/__init__.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/testing/__init__.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat/utils/__init__.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat.egg-info/SOURCES.txt +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat.egg-info/dependency_links.txt +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat.egg-info/requires.txt +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/audformat.egg-info/top_level.txt +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/benchmarks/benchmark_union.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/accessing-data.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api/audformat.define.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api/audformat.errors.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api/audformat.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api/audformat.testing.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api/audformat.utils.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api-src/audformat.define.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api-src/audformat.errors.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api-src/audformat.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api-src/audformat.testing.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/api-src/audformat.utils.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/changelog.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/combine-tables.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/conf.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/conftest.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/contributing.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/create-database.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-conventions.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-example.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-format.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-header.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-introduction.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/data-tables.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/emodb-example.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/genindex.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/index.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/install.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/map-scheme.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/pics/audformat-database.dot +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/pics/audformat-misc-table.dot +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/pics/audformat-table.dot +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/docs/update-database.rst +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/pyproject.toml +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/setup.cfg +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/conftest.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_attachment.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_common.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_database.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_eq.py +0 -0
- {audformat-1.3.3 → audformat-1.3.4}/tests/test_errors.py +0 -0
|
@@ -7,6 +7,12 @@ The format is based on `Keep a Changelog`_,
|
|
|
7
7
|
and this project adheres to `Semantic Versioning`_.
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
Version 1.3.4 (2026-02-18)
|
|
11
|
+
--------------------------
|
|
12
|
+
|
|
13
|
+
* Added: support for ``pandas >=3.0``
|
|
14
|
+
|
|
15
|
+
|
|
10
16
|
Version 1.3.3 (2026-01-12)
|
|
11
17
|
--------------------------
|
|
12
18
|
|
|
@@ -362,6 +362,21 @@ def to_audformat_dtype(dtype: str | type) -> str:
|
|
|
362
362
|
return define.DataType.OBJECT
|
|
363
363
|
|
|
364
364
|
|
|
365
|
+
def to_categorical_dtype(labels: list[object]) -> pd.api.types.CategoricalDtype:
|
|
366
|
+
"""Create categorical dtype for labels."""
|
|
367
|
+
if len(labels) > 0:
|
|
368
|
+
if all(isinstance(label, int) for label in labels):
|
|
369
|
+
# allow nullable
|
|
370
|
+
labels = pd.array(labels, dtype="int64")
|
|
371
|
+
elif all(isinstance(label, str) for label in labels):
|
|
372
|
+
# object for string categories
|
|
373
|
+
labels = pd.Index(labels, dtype="object")
|
|
374
|
+
return pd.api.types.CategoricalDtype(
|
|
375
|
+
categories=labels,
|
|
376
|
+
ordered=False,
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
|
|
365
380
|
def to_pandas_dtype(dtype: str) -> str | None:
|
|
366
381
|
r"""Convert audformat to pandas dtype.
|
|
367
382
|
|
|
@@ -7,6 +7,7 @@ import itertools
|
|
|
7
7
|
import os
|
|
8
8
|
import shutil
|
|
9
9
|
|
|
10
|
+
import numpy as np
|
|
10
11
|
import oyaml as yaml
|
|
11
12
|
|
|
12
13
|
|
|
@@ -696,12 +697,16 @@ class Database(HeaderBase):
|
|
|
696
697
|
ys.append(y)
|
|
697
698
|
|
|
698
699
|
def dtypes_of_categories(objs):
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
700
|
+
cat_dtypes = []
|
|
701
|
+
for obj in objs:
|
|
702
|
+
if isinstance(obj.dtype, pd.CategoricalDtype):
|
|
703
|
+
# Normalize string-like categorical dtypes to object
|
|
704
|
+
cat_dtype = obj.dtype.categories.dtype
|
|
705
|
+
if isinstance(cat_dtype, pd.StringDtype):
|
|
706
|
+
cat_dtype = np.dtype("O")
|
|
707
|
+
cat_dtypes.append(cat_dtype)
|
|
708
|
+
# Deduplicate and sort for consistent ordering
|
|
709
|
+
return sorted(list(set(cat_dtypes)), key=str)
|
|
705
710
|
|
|
706
711
|
def empty_frame(name):
|
|
707
712
|
return pd.DataFrame(
|
|
@@ -823,7 +828,7 @@ class Database(HeaderBase):
|
|
|
823
828
|
raise TypeError( # pragma: nocover
|
|
824
829
|
f"Cannot join labels for scheme '{requested_scheme}' "
|
|
825
830
|
"with different data types: "
|
|
826
|
-
f"{', '.join(dtypes)}"
|
|
831
|
+
f"{', '.join(str(d) for d in dtypes)}"
|
|
827
832
|
)
|
|
828
833
|
dtype = dtypes[0]
|
|
829
834
|
# Convert everything to categorical data
|
|
@@ -832,6 +837,12 @@ class Database(HeaderBase):
|
|
|
832
837
|
ys[n] = y.astype(
|
|
833
838
|
pd.CategoricalDtype(y.array.dropna().unique().astype(dtype))
|
|
834
839
|
)
|
|
840
|
+
# Normalize string-like categorical dtypes to object
|
|
841
|
+
for n, y in enumerate(ys):
|
|
842
|
+
cat_dtype = y.dtype.categories.dtype
|
|
843
|
+
if isinstance(cat_dtype, pd.StringDtype):
|
|
844
|
+
new_categories = y.dtype.categories.astype("object")
|
|
845
|
+
ys[n] = y.astype(pd.CategoricalDtype(new_categories))
|
|
835
846
|
# Find union of categorical data
|
|
836
847
|
data = [y.array for y in ys]
|
|
837
848
|
try:
|
|
@@ -27,9 +27,13 @@ def to_array(value: object) -> list | np.ndarray:
|
|
|
27
27
|
def to_timedelta(times):
|
|
28
28
|
r"""Convert time value to pd.Timedelta."""
|
|
29
29
|
try:
|
|
30
|
-
|
|
30
|
+
result = pd.to_timedelta(times, unit="s")
|
|
31
31
|
except ValueError: # catches values like '1s'
|
|
32
|
-
|
|
32
|
+
result = pd.to_timedelta(times)
|
|
33
|
+
if isinstance(result, pd.Timedelta):
|
|
34
|
+
return result.as_unit("ns")
|
|
35
|
+
else:
|
|
36
|
+
return result.astype("timedelta64[ns]")
|
|
33
37
|
|
|
34
38
|
|
|
35
39
|
def assert_index(
|
|
@@ -100,12 +104,12 @@ def assert_index(
|
|
|
100
104
|
"Index not conform to audformat. "
|
|
101
105
|
"Level 'file' must contain values of type 'string'."
|
|
102
106
|
)
|
|
103
|
-
if not pd.api.types.
|
|
107
|
+
if not pd.api.types.is_timedelta64_ns_dtype(obj.levels[1].dtype):
|
|
104
108
|
raise ValueError(
|
|
105
109
|
"Index not conform to audformat. "
|
|
106
110
|
"Level 'start' must contain values of type 'timedelta64[ns]'."
|
|
107
111
|
)
|
|
108
|
-
if not pd.api.types.
|
|
112
|
+
if not pd.api.types.is_timedelta64_ns_dtype(obj.levels[2].dtype):
|
|
109
113
|
raise ValueError(
|
|
110
114
|
"Index not conform to audformat. "
|
|
111
115
|
"Level 'end' must contain values of type 'timedelta64[ns]'."
|
|
@@ -287,16 +287,9 @@ class Scheme(common.HeaderBase):
|
|
|
287
287
|
"""
|
|
288
288
|
if self.labels is not None:
|
|
289
289
|
labels = self._labels_to_list()
|
|
290
|
-
|
|
291
|
-
# allow nullable
|
|
292
|
-
labels = pd.array(labels, dtype="int64")
|
|
293
|
-
dtype = pd.api.types.CategoricalDtype(
|
|
294
|
-
categories=labels,
|
|
295
|
-
ordered=False,
|
|
296
|
-
)
|
|
290
|
+
return common.to_categorical_dtype(labels)
|
|
297
291
|
else:
|
|
298
|
-
|
|
299
|
-
return dtype
|
|
292
|
+
return common.to_pandas_dtype(self.dtype)
|
|
300
293
|
|
|
301
294
|
def replace_labels(
|
|
302
295
|
self,
|
|
@@ -19,6 +19,7 @@ from audformat.core import utils
|
|
|
19
19
|
from audformat.core.column import Column
|
|
20
20
|
from audformat.core.common import HeaderBase
|
|
21
21
|
from audformat.core.common import HeaderDict
|
|
22
|
+
from audformat.core.common import to_categorical_dtype
|
|
22
23
|
from audformat.core.common import to_pandas_dtype
|
|
23
24
|
from audformat.core.errors import BadIdError
|
|
24
25
|
from audformat.core.index import filewise_index
|
|
@@ -981,7 +982,7 @@ class Base(HeaderBase):
|
|
|
981
982
|
and (self.db.schemes[column.scheme_id].dtype == define.DataType.STRING)
|
|
982
983
|
and df[column_id].dtype == "object"
|
|
983
984
|
):
|
|
984
|
-
df[column_id] = df[column_id].astype("string"
|
|
985
|
+
df[column_id] = df[column_id].astype("string")
|
|
985
986
|
# Fix index entries as well
|
|
986
987
|
df.index = _maybe_convert_dtype_to_string(df.index)
|
|
987
988
|
|
|
@@ -1004,11 +1005,13 @@ class Base(HeaderBase):
|
|
|
1004
1005
|
df: dataframe,
|
|
1005
1006
|
convert_all: if ``False``,
|
|
1006
1007
|
converts all columns with
|
|
1007
|
-
``"object"
|
|
1008
|
+
``"object"``,
|
|
1009
|
+
and ``"time"``
|
|
1010
|
+
audformat dtype,
|
|
1008
1011
|
and all columns with a scheme with labels.
|
|
1009
1012
|
If ``"True"``,
|
|
1010
1013
|
it converts additionally all columns with
|
|
1011
|
-
``"bool"``,
|
|
1014
|
+
``"bool"``, and ``"int"`` audformat dtypes
|
|
1012
1015
|
|
|
1013
1016
|
Returns:
|
|
1014
1017
|
dataframe with converted dtypes
|
|
@@ -1066,21 +1069,15 @@ class Base(HeaderBase):
|
|
|
1066
1069
|
df[column] = df[column].astype("boolean")
|
|
1067
1070
|
for column in int_columns:
|
|
1068
1071
|
df[column] = df[column].astype("Int64")
|
|
1069
|
-
|
|
1070
|
-
|
|
1072
|
+
for column in time_columns:
|
|
1073
|
+
df[column] = df[column].astype("timedelta64[ns]")
|
|
1071
1074
|
for column in object_columns:
|
|
1072
1075
|
df[column] = df[column].astype("object")
|
|
1073
1076
|
df[column] = df[column].replace(pd.NA, None)
|
|
1074
1077
|
for column in labeled_columns:
|
|
1075
1078
|
scheme = self.db.schemes[self.columns[column].scheme_id]
|
|
1076
1079
|
labels = scheme._labels_to_list()
|
|
1077
|
-
|
|
1078
|
-
# allow nullable
|
|
1079
|
-
labels = pd.array(labels, dtype="int64")
|
|
1080
|
-
dtype = pd.api.types.CategoricalDtype(
|
|
1081
|
-
categories=labels,
|
|
1082
|
-
ordered=False,
|
|
1083
|
-
)
|
|
1080
|
+
dtype = to_categorical_dtype(labels)
|
|
1084
1081
|
df[column] = df[column].astype(dtype)
|
|
1085
1082
|
return df
|
|
1086
1083
|
|
|
@@ -17,6 +17,7 @@ from audformat.core.index import filewise_index
|
|
|
17
17
|
from audformat.core.index import is_filewise_index
|
|
18
18
|
from audformat.core.index import is_segmented_index
|
|
19
19
|
from audformat.core.index import segmented_index
|
|
20
|
+
from audformat.core.index import to_timedelta
|
|
20
21
|
from audformat.core.media import Media
|
|
21
22
|
from audformat.core.rater import Rater
|
|
22
23
|
from audformat.core.scheme import Scheme
|
|
@@ -115,7 +116,7 @@ def add_table(
|
|
|
115
116
|
|
|
116
117
|
"""
|
|
117
118
|
if isinstance(file_duration, str):
|
|
118
|
-
file_duration =
|
|
119
|
+
file_duration = to_timedelta(file_duration)
|
|
119
120
|
|
|
120
121
|
audio_format = "wav"
|
|
121
122
|
if media_id and db.media[media_id].format:
|
|
@@ -143,7 +144,7 @@ def add_table(
|
|
|
143
144
|
|
|
144
145
|
for file in files:
|
|
145
146
|
times = [
|
|
146
|
-
|
|
147
|
+
random.random() * file_duration
|
|
147
148
|
for _ in range(num_segments_per_file * 2)
|
|
148
149
|
]
|
|
149
150
|
times.sort()
|
|
@@ -725,8 +725,51 @@ def hash(
|
|
|
725
725
|
df = obj.to_frame().reset_index()
|
|
726
726
|
else:
|
|
727
727
|
df = obj.reset_index()
|
|
728
|
-
|
|
729
|
-
|
|
728
|
+
|
|
729
|
+
# Ensure column names are strings for pyarrow compatibility
|
|
730
|
+
# (empty index converted to DataFrame may have integer column names)
|
|
731
|
+
df.columns = [str(c) for c in df.columns]
|
|
732
|
+
|
|
733
|
+
# Normalize string columns to object dtype for consistent hashing
|
|
734
|
+
# (pandas 3.0 uses "string" dtype which maps to pyarrow "large_string",
|
|
735
|
+
# while "object" dtype maps to pyarrow "string")
|
|
736
|
+
# For empty DataFrames, we also need to specify an explicit schema
|
|
737
|
+
# because pyarrow infers "null" type for empty object columns
|
|
738
|
+
schema_fields = []
|
|
739
|
+
for col in df.columns:
|
|
740
|
+
if pd.api.types.is_string_dtype(df[col].dtype):
|
|
741
|
+
df[col] = df[col].astype("object")
|
|
742
|
+
schema_fields.append((col, pa.string()))
|
|
743
|
+
elif isinstance(df[col].dtype, pd.CategoricalDtype):
|
|
744
|
+
# Normalize categorical with string categories to object
|
|
745
|
+
cat_dtype = df[col].dtype.categories.dtype
|
|
746
|
+
if pd.api.types.is_string_dtype(cat_dtype):
|
|
747
|
+
new_categories = df[col].dtype.categories.astype("object")
|
|
748
|
+
ordered = df[col].dtype.ordered
|
|
749
|
+
df[col] = df[col].astype(
|
|
750
|
+
pd.CategoricalDtype(new_categories, ordered=ordered)
|
|
751
|
+
)
|
|
752
|
+
schema_fields.append((col, None))
|
|
753
|
+
else:
|
|
754
|
+
# Let pyarrow infer
|
|
755
|
+
schema_fields.append((col, None))
|
|
756
|
+
# Build schema for columns that need explicit types
|
|
757
|
+
if len(df) == 0 and any(f[1] is not None for f in schema_fields):
|
|
758
|
+
# For empty DataFrames with index of type string/object,
|
|
759
|
+
# specify schema explicitly
|
|
760
|
+
schema = pa.schema(
|
|
761
|
+
[
|
|
762
|
+
(
|
|
763
|
+
name,
|
|
764
|
+
typ if typ is not None else pa.from_numpy_dtype(df[name].dtype),
|
|
765
|
+
)
|
|
766
|
+
for name, typ in schema_fields
|
|
767
|
+
]
|
|
768
|
+
)
|
|
769
|
+
table = pa.Table.from_pandas(df, preserve_index=False, schema=schema)
|
|
770
|
+
else:
|
|
771
|
+
# Handle column names and dtypes
|
|
772
|
+
table = pa.Table.from_pandas(df, preserve_index=False)
|
|
730
773
|
schema_str = table.schema.to_string(
|
|
731
774
|
# schema.metadata contains pandas related information,
|
|
732
775
|
# and the used pyarrow and pandas version,
|
|
@@ -1030,7 +1073,7 @@ def iter_by_file(
|
|
|
1030
1073
|
('f1', MultiIndex([('f1', '0 days 00:00:00', '0 days 00:00:02'),
|
|
1031
1074
|
('f1', '0 days 00:00:01', '0 days 00:00:03')],
|
|
1032
1075
|
names=['file', 'start', 'end']))
|
|
1033
|
-
>>> obj = pd.Series(["a", "b", "b"], index)
|
|
1076
|
+
>>> obj = pd.Series(["a", "b", "b"], index, dtype="object")
|
|
1034
1077
|
>>> next(iter_by_file(obj))
|
|
1035
1078
|
('f1', file start end
|
|
1036
1079
|
f1 0 days 00:00:00 0 days 00:00:02 a
|
|
@@ -1479,14 +1522,14 @@ def set_index_dtypes(
|
|
|
1479
1522
|
index with new dtypes
|
|
1480
1523
|
|
|
1481
1524
|
Examples:
|
|
1482
|
-
>>> index1 = pd.Index(["a", "b"])
|
|
1525
|
+
>>> index1 = pd.Index(["a", "b"], dtype="object")
|
|
1483
1526
|
>>> index1
|
|
1484
1527
|
Index(['a', 'b'], dtype='object')
|
|
1485
1528
|
>>> index2 = set_index_dtypes(index1, "string")
|
|
1486
1529
|
>>> index2
|
|
1487
1530
|
Index(['a', 'b'], dtype='string')
|
|
1488
1531
|
>>> index3 = pd.MultiIndex.from_arrays(
|
|
1489
|
-
... [["a", "b"], [1, 2]],
|
|
1532
|
+
... [pd.Index(["a", "b"], dtype="object"), [1, 2]],
|
|
1490
1533
|
... names=["level1", "level2"],
|
|
1491
1534
|
... )
|
|
1492
1535
|
>>> index3.dtypes
|
|
@@ -1498,11 +1541,6 @@ def set_index_dtypes(
|
|
|
1498
1541
|
level1 object
|
|
1499
1542
|
level2 float64
|
|
1500
1543
|
dtype: object
|
|
1501
|
-
>>> index5 = set_index_dtypes(index3, "string")
|
|
1502
|
-
>>> index5.dtypes
|
|
1503
|
-
level1 string[python]
|
|
1504
|
-
level2 string[python]
|
|
1505
|
-
dtype: object
|
|
1506
1544
|
|
|
1507
1545
|
"""
|
|
1508
1546
|
levels = index.names if isinstance(index, pd.MultiIndex) else [index.name]
|
|
@@ -1522,6 +1560,17 @@ def set_index_dtypes(
|
|
|
1522
1560
|
if len(dtypes) == 0:
|
|
1523
1561
|
return index
|
|
1524
1562
|
|
|
1563
|
+
def dtypes_equal(dt1, dt2):
|
|
1564
|
+
# Normalize representations to actual dtype objects
|
|
1565
|
+
dt1 = pd.api.types.pandas_dtype(dt1)
|
|
1566
|
+
dt2 = pd.api.types.pandas_dtype(dt2)
|
|
1567
|
+
if dt1 != dt2:
|
|
1568
|
+
return False
|
|
1569
|
+
# Different string dtypes should return False
|
|
1570
|
+
if isinstance(dt1, pd.StringDtype):
|
|
1571
|
+
return dt1.na_value is dt2.na_value
|
|
1572
|
+
return dt1 == dt2
|
|
1573
|
+
|
|
1525
1574
|
if isinstance(index, pd.MultiIndex):
|
|
1526
1575
|
# MultiIndex
|
|
1527
1576
|
if any([len(index.levels[index.names.index(level)]) == 0 for level in dtypes]):
|
|
@@ -1529,11 +1578,11 @@ def set_index_dtypes(
|
|
|
1529
1578
|
# so we convert to a dataframe instead
|
|
1530
1579
|
df = index.to_frame()
|
|
1531
1580
|
for level, dtype in dtypes.items():
|
|
1532
|
-
if dtype
|
|
1581
|
+
if not dtypes_equal(dtype, df[level].dtype):
|
|
1533
1582
|
if pd.api.types.is_timedelta64_dtype(dtype):
|
|
1534
1583
|
# avoid: TypeError: Cannot cast DatetimeArray
|
|
1535
1584
|
# to dtype timedelta64[ns]
|
|
1536
|
-
df[level] = pd.to_timedelta(list(df[level]))
|
|
1585
|
+
df[level] = pd.to_timedelta(list(df[level])).astype(dtype)
|
|
1537
1586
|
else:
|
|
1538
1587
|
df[level] = df[level].astype(dtype)
|
|
1539
1588
|
index = pd.MultiIndex.from_frame(df)
|
|
@@ -1544,7 +1593,7 @@ def set_index_dtypes(
|
|
|
1544
1593
|
# hence we access the data directly with
|
|
1545
1594
|
# index.levels[idx]
|
|
1546
1595
|
idx = index.names.index(level)
|
|
1547
|
-
if dtype
|
|
1596
|
+
if not dtypes_equal(dtype, index.levels[idx].dtype):
|
|
1548
1597
|
index = index.set_levels(
|
|
1549
1598
|
index.levels[idx].astype(dtype),
|
|
1550
1599
|
level=level,
|
|
@@ -1553,7 +1602,7 @@ def set_index_dtypes(
|
|
|
1553
1602
|
else:
|
|
1554
1603
|
# Index
|
|
1555
1604
|
dtype = next(iter(dtypes.values()))
|
|
1556
|
-
if dtype
|
|
1605
|
+
if not dtypes_equal(dtype, index.dtype):
|
|
1557
1606
|
index = index.astype(dtype)
|
|
1558
1607
|
|
|
1559
1608
|
return index
|
|
@@ -507,7 +507,7 @@ def db_scheme_with_misc_table():
|
|
|
507
507
|
index=audformat.filewise_index(["f1", "f2", "f3"]),
|
|
508
508
|
name="column",
|
|
509
509
|
dtype=pd.CategoricalDtype(
|
|
510
|
-
categories=["a", "b", "c"],
|
|
510
|
+
categories=pd.Index(["a", "b", "c"], dtype="object"),
|
|
511
511
|
ordered=False,
|
|
512
512
|
),
|
|
513
513
|
),
|
|
@@ -598,7 +598,7 @@ def db_scheme_with_misc_table():
|
|
|
598
598
|
index=audformat.filewise_index(["f1", "f2", "f3"]),
|
|
599
599
|
name="column",
|
|
600
600
|
dtype=pd.CategoricalDtype(
|
|
601
|
-
categories=["a", "b", "c"],
|
|
601
|
+
categories=pd.Index(["a", "b", "c"], dtype="object"),
|
|
602
602
|
ordered=False,
|
|
603
603
|
),
|
|
604
604
|
),
|
|
@@ -621,7 +621,7 @@ def db_scheme_with_misc_table():
|
|
|
621
621
|
index=audformat.filewise_index(["f1", "f2", "f3"]),
|
|
622
622
|
name="str-categories",
|
|
623
623
|
dtype=pd.CategoricalDtype(
|
|
624
|
-
categories=["female", "male"],
|
|
624
|
+
categories=pd.Index(["female", "male"], dtype="object"),
|
|
625
625
|
ordered=False,
|
|
626
626
|
),
|
|
627
627
|
),
|
|
@@ -511,7 +511,7 @@ def wrong_scheme_labels_db(tmpdir):
|
|
|
511
511
|
]
|
|
512
512
|
),
|
|
513
513
|
dtype=pd.CategoricalDtype(
|
|
514
|
-
["w1", "w2", "w3"],
|
|
514
|
+
pd.Index(["w1", "w2", "w3"], dtype="object"),
|
|
515
515
|
ordered=False,
|
|
516
516
|
),
|
|
517
517
|
),
|
|
@@ -603,7 +603,7 @@ def wrong_scheme_labels_db(tmpdir):
|
|
|
603
603
|
[0.2, 0.2, 0.5, 0.7],
|
|
604
604
|
),
|
|
605
605
|
dtype=pd.CategoricalDtype(
|
|
606
|
-
["s1", "s2", "s3"],
|
|
606
|
+
pd.Index(["s1", "s2", "s3"], dtype="object"),
|
|
607
607
|
ordered=False,
|
|
608
608
|
),
|
|
609
609
|
name="speaker",
|
|
@@ -849,7 +849,7 @@ def wrong_scheme_labels_db(tmpdir):
|
|
|
849
849
|
},
|
|
850
850
|
index=audformat.filewise_index(["f1.wav", "f2.wav"]),
|
|
851
851
|
dtype=pd.CategoricalDtype(
|
|
852
|
-
["female", "male"],
|
|
852
|
+
pd.Index(["female", "male"], dtype="str"),
|
|
853
853
|
ordered=False,
|
|
854
854
|
),
|
|
855
855
|
),
|
|
@@ -1253,7 +1253,7 @@ def test_database_get_aggregate_and_modify_function(
|
|
|
1253
1253
|
["s1"],
|
|
1254
1254
|
index=audformat.filewise_index(["f1.wav"]),
|
|
1255
1255
|
dtype=pd.CategoricalDtype(
|
|
1256
|
-
["s1", "s2", "s3"],
|
|
1256
|
+
pd.Index(["s1", "s2", "s3"], dtype="object"),
|
|
1257
1257
|
ordered=False,
|
|
1258
1258
|
),
|
|
1259
1259
|
name="speaker",
|
|
@@ -1825,3 +1825,56 @@ def test_database_get_errors(
|
|
|
1825
1825
|
tables=tables,
|
|
1826
1826
|
original_column_names=original_column_names,
|
|
1827
1827
|
)
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
def test_get_mixed_str_and_object_categorical_dtype(tmpdir):
|
|
1831
|
+
"""Test that mixing 'str' and 'object' categorical dtypes normalizes to 'object'."""
|
|
1832
|
+
db = audformat.Database("test")
|
|
1833
|
+
|
|
1834
|
+
# Create scheme with string labels
|
|
1835
|
+
db.schemes["label"] = audformat.Scheme(
|
|
1836
|
+
"str",
|
|
1837
|
+
labels=["a", "b", "c"],
|
|
1838
|
+
)
|
|
1839
|
+
|
|
1840
|
+
# Create two tables with the same scheme
|
|
1841
|
+
index1 = audformat.filewise_index(["f1.wav", "f2.wav"])
|
|
1842
|
+
db["table1"] = audformat.Table(index1)
|
|
1843
|
+
db["table1"]["label"] = audformat.Column(scheme_id="label")
|
|
1844
|
+
db["table1"]["label"].set(["a", "b"])
|
|
1845
|
+
|
|
1846
|
+
index2 = audformat.filewise_index(["f3.wav", "f4.wav"])
|
|
1847
|
+
db["table2"] = audformat.Table(index2)
|
|
1848
|
+
db["table2"]["label"] = audformat.Column(scheme_id="label")
|
|
1849
|
+
db["table2"]["label"].set(["b", "c"])
|
|
1850
|
+
|
|
1851
|
+
# Manually set different category dtypes (object + string)
|
|
1852
|
+
# (even if this might not happen in reality)
|
|
1853
|
+
df1 = db["table1"].df
|
|
1854
|
+
df2 = db["table2"].df
|
|
1855
|
+
df1["label"] = df1["label"].astype(
|
|
1856
|
+
pd.CategoricalDtype(
|
|
1857
|
+
pd.Index(["a", "b", "c"], dtype="object"),
|
|
1858
|
+
ordered=False,
|
|
1859
|
+
)
|
|
1860
|
+
)
|
|
1861
|
+
db["table1"]._df = df1
|
|
1862
|
+
df2["label"] = df2["label"].astype(
|
|
1863
|
+
pd.CategoricalDtype(
|
|
1864
|
+
pd.Index(["a", "b", "c"], dtype="string"),
|
|
1865
|
+
ordered=False,
|
|
1866
|
+
)
|
|
1867
|
+
)
|
|
1868
|
+
db["table2"]._df = df2
|
|
1869
|
+
|
|
1870
|
+
# Get combined data - this should work without errors
|
|
1871
|
+
result = db.get("label")
|
|
1872
|
+
|
|
1873
|
+
# Verify result contains all labels
|
|
1874
|
+
assert list(result["label"]) == ["a", "b", "b", "c"]
|
|
1875
|
+
|
|
1876
|
+
# Verify the resulting categorical has object dtype for categories
|
|
1877
|
+
result_dtype = result["label"].dtype
|
|
1878
|
+
assert isinstance(result_dtype, pd.CategoricalDtype)
|
|
1879
|
+
# Categories should be normalized to object dtype
|
|
1880
|
+
assert result_dtype.categories.dtype == np.dtype("O")
|
|
@@ -69,8 +69,8 @@ def to_array(value):
|
|
|
69
69
|
pd.MultiIndex.from_arrays(
|
|
70
70
|
[
|
|
71
71
|
[1, 2],
|
|
72
|
-
pd.to_timedelta([0.0, 1.0], unit="s"),
|
|
73
|
-
pd.to_timedelta([1.0, 2.0], unit="s"),
|
|
72
|
+
pd.to_timedelta([0.0, 1.0], unit="s").astype("timedelta64[ns]"),
|
|
73
|
+
pd.to_timedelta([1.0, 2.0], unit="s").astype("timedelta64[ns]"),
|
|
74
74
|
],
|
|
75
75
|
names=["file", "start", "end"],
|
|
76
76
|
),
|
|
@@ -81,7 +81,7 @@ def to_array(value):
|
|
|
81
81
|
[
|
|
82
82
|
["f1", "f2"],
|
|
83
83
|
[0.0, 1.0],
|
|
84
|
-
pd.to_timedelta([1.0, 2.0], unit="s"),
|
|
84
|
+
pd.to_timedelta([1.0, 2.0], unit="s").astype("timedelta64[ns]"),
|
|
85
85
|
],
|
|
86
86
|
names=["file", "start", "end"],
|
|
87
87
|
),
|
|
@@ -91,13 +91,46 @@ def to_array(value):
|
|
|
91
91
|
pd.MultiIndex.from_arrays(
|
|
92
92
|
[
|
|
93
93
|
["f1", "f2"],
|
|
94
|
-
pd.to_timedelta([0.0, 1.0], unit="s"),
|
|
94
|
+
pd.to_timedelta([0.0, 1.0], unit="s").astype("timedelta64[ns]"),
|
|
95
95
|
[1.0, 2.0],
|
|
96
96
|
],
|
|
97
97
|
names=["file", "start", "end"],
|
|
98
98
|
),
|
|
99
99
|
marks=pytest.mark.xfail(raises=ValueError),
|
|
100
100
|
),
|
|
101
|
+
pytest.param( # invalid timedelta unit
|
|
102
|
+
pd.MultiIndex.from_arrays(
|
|
103
|
+
[
|
|
104
|
+
["f1", "f2"],
|
|
105
|
+
pd.to_timedelta([0.0, 1.0], unit="s").astype("timedelta64[s]"),
|
|
106
|
+
pd.to_timedelta([1.0, 2.0], unit="s").astype("timedelta64[ns]"),
|
|
107
|
+
],
|
|
108
|
+
names=["file", "start", "end"],
|
|
109
|
+
),
|
|
110
|
+
marks=pytest.mark.xfail(raises=ValueError),
|
|
111
|
+
),
|
|
112
|
+
pytest.param( # invalid timedelta unit
|
|
113
|
+
pd.MultiIndex.from_arrays(
|
|
114
|
+
[
|
|
115
|
+
["f1", "f2"],
|
|
116
|
+
pd.to_timedelta([0.0, 1.0], unit="s").astype("timedelta64[ns]"),
|
|
117
|
+
pd.to_timedelta([1.0, 2.0], unit="s").astype("timedelta64[s]"),
|
|
118
|
+
],
|
|
119
|
+
names=["file", "start", "end"],
|
|
120
|
+
),
|
|
121
|
+
marks=pytest.mark.xfail(raises=ValueError),
|
|
122
|
+
),
|
|
123
|
+
pytest.param( # invalid timedelta unit
|
|
124
|
+
pd.MultiIndex.from_arrays(
|
|
125
|
+
[
|
|
126
|
+
["f1", "f2"],
|
|
127
|
+
pd.to_timedelta([0.0, 1.0], unit="s").astype("timedelta64[s]"),
|
|
128
|
+
pd.to_timedelta([1.0, 2.0], unit="s").astype("timedelta64[s]"),
|
|
129
|
+
],
|
|
130
|
+
names=["file", "start", "end"],
|
|
131
|
+
),
|
|
132
|
+
marks=pytest.mark.xfail(raises=ValueError),
|
|
133
|
+
),
|
|
101
134
|
],
|
|
102
135
|
)
|
|
103
136
|
def test_assert_index(obj):
|
|
@@ -262,6 +295,27 @@ def test_create_segmented_index(files, starts, ends):
|
|
|
262
295
|
] * len(files)
|
|
263
296
|
|
|
264
297
|
|
|
298
|
+
@pytest.mark.parametrize(
|
|
299
|
+
"files, starts, ends",
|
|
300
|
+
[
|
|
301
|
+
# normal case with sub-second values
|
|
302
|
+
(["f1.wav"], [0.001], [0.002]),
|
|
303
|
+
# NaT in ends
|
|
304
|
+
(["f1.wav"], [0], [pd.NaT]),
|
|
305
|
+
# NaT in starts and ends
|
|
306
|
+
(["f1.wav"], [pd.NaT], [pd.NaT]),
|
|
307
|
+
# empty index
|
|
308
|
+
(None, None, None),
|
|
309
|
+
],
|
|
310
|
+
)
|
|
311
|
+
def test_segmented_index_dtype(files, starts, ends):
|
|
312
|
+
"""Check for correct dtypes in segmented_index."""
|
|
313
|
+
index = audformat.segmented_index(files, starts=starts, ends=ends)
|
|
314
|
+
assert index.get_level_values("file").dtype == pd.StringDtype(na_value=pd.NA)
|
|
315
|
+
assert index.get_level_values("start").dtype == "timedelta64[ns]"
|
|
316
|
+
assert index.get_level_values("end").dtype == "timedelta64[ns]"
|
|
317
|
+
|
|
318
|
+
|
|
265
319
|
@pytest.mark.parametrize(
|
|
266
320
|
"index, index_type",
|
|
267
321
|
[
|