From 95fbce1c4a13b908e228176cc2ba19702b741769 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Wed, 11 Sep 2024 21:16:37 -0700 Subject: [PATCH] add id to index name on aligneddynamictable correctly --- nwb_linkml/src/nwb_linkml/includes/hdmf.py | 14 +++++++------- nwb_linkml/tests/test_includes/test_hdmf.py | 12 ++++++------ .../hdmf_common/v1_1_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_1_2/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_1_3/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_2_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_2_1/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_3_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_4_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_5_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_5_1/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_6_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_7_0/hdmf_common_table.py | 14 +++++++------- .../hdmf_common/v1_8_0/hdmf_common_table.py | 14 +++++++------- 14 files changed, 97 insertions(+), 97 deletions(-) diff --git a/nwb_linkml/src/nwb_linkml/includes/hdmf.py b/nwb_linkml/src/nwb_linkml/includes/hdmf.py index b64e0f1..7a7d294 100644 --- a/nwb_linkml/src/nwb_linkml/includes/hdmf.py +++ b/nwb_linkml/src/nwb_linkml/includes/hdmf.py @@ -627,23 +627,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -651,8 +652,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_linkml/tests/test_includes/test_hdmf.py b/nwb_linkml/tests/test_includes/test_hdmf.py index 7868b0b..a8b14b7 100644 --- a/nwb_linkml/tests/test_includes/test_hdmf.py +++ b/nwb_linkml/tests/test_includes/test_hdmf.py @@ -551,13 +551,13 @@ def test_aligned_dynamictable_indexing(aligned_table): row.columns == pd.MultiIndex.from_tuples( [ - ("table1", "index"), + ("table1", "id"), ("table1", "col1"), ("table1", "col2"), - ("table2", "index"), + ("table2", "id"), ("table2", "col3"), ("table2", "col4"), - ("table3", "index"), + ("table3", "id"), ("table3", "col5"), ("table3", "col6"), ] @@ -754,11 +754,11 @@ def test_aligned_dynamictable_ictable(intracellular_recordings_table): rows.columns == pd.MultiIndex.from_tuples( [ - ("electrodes", "index"), + ("electrodes", "id"), ("electrodes", "electrode"), - ("stimuli", "index"), + ("stimuli", "id"), ("stimuli", "stimulus"), - ("responses", "index"), + ("responses", "id"), ("responses", "response"), ] ) diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_0/hdmf_common_table.py index 00ced23..e52b294 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_0/hdmf_common_table.py @@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_2/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_2/hdmf_common_table.py index 2bc9dde..9065b81 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_2/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_2/hdmf_common_table.py @@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_3/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_3/hdmf_common_table.py index cf7c150..749fab9 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_3/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_1_3/hdmf_common_table.py @@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_0/hdmf_common_table.py index 663d58b..fdd6bcc 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_1/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_1/hdmf_common_table.py index b61981d..cc9029d 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_1/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_2_1/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_3_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_3_0/hdmf_common_table.py index 2d1973b..a55c212 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_3_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_3_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_4_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_4_0/hdmf_common_table.py index 804d424..a730ec1 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_4_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_4_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_0/hdmf_common_table.py index 9e8c2ad..27a287c 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_1/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_1/hdmf_common_table.py index d3c2f22..3112a4f 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_1/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_5_1/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_6_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_6_0/hdmf_common_table.py index 7dc1868..0759b51 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_6_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_6_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_7_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_7_0/hdmf_common_table.py index 0780f0e..e805fe7 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_7_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_7_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: diff --git a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py index 024e442..8f0d610 100644 --- a/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py +++ b/nwb_models/src/nwb_models/models/pydantic/hdmf_common/v1_8_0/hdmf_common_table.py @@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel): ids = self.id[item] if not isinstance(ids, Iterable): ids = pd.Series([ids]) - ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) - tables = [ids] + ids = pd.Index(data=ids, name="id") + tables = [] for category_name, category in self._categories.items(): table = category[item] if isinstance(table, pd.DataFrame): table = table.reset_index() + table.index = ids elif isinstance(table, np.ndarray): - table = pd.DataFrame({category_name: [table]}, index=ids.index) + table = pd.DataFrame({category_name: [table]}, index=ids) elif isinstance(table, Iterable): - table = pd.DataFrame({category_name: table}, index=ids.index) + table = pd.DataFrame({category_name: table}, index=ids) else: raise ValueError( f"Don't know how to construct category table for {category_name}" ) tables.append(table) - names = [self.name] + self.categories + # names = [self.name] + self.categories # construct below in case we need to support array indexing in the future else: raise ValueError( @@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel): "need an int, string, slice, ndarray, or tuple[int | slice, str]" ) - df = pd.concat(tables, axis=1, keys=names) - df.set_index((self.name, "id"), drop=True, inplace=True) + df = pd.concat(tables, axis=1, keys=self.categories) return df def __getattr__(self, item: str) -> Any: