add id to index name on aligneddynamictable correctly

This commit is contained in:
sneakers-the-rat 2024-09-11 21:16:37 -07:00
parent 91b2abf07e
commit 95fbce1c4a
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D
14 changed files with 97 additions and 97 deletions

View file

@ -627,23 +627,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -651,8 +652,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -551,13 +551,13 @@ def test_aligned_dynamictable_indexing(aligned_table):
row.columns row.columns
== pd.MultiIndex.from_tuples( == pd.MultiIndex.from_tuples(
[ [
("table1", "index"), ("table1", "id"),
("table1", "col1"), ("table1", "col1"),
("table1", "col2"), ("table1", "col2"),
("table2", "index"), ("table2", "id"),
("table2", "col3"), ("table2", "col3"),
("table2", "col4"), ("table2", "col4"),
("table3", "index"), ("table3", "id"),
("table3", "col5"), ("table3", "col5"),
("table3", "col6"), ("table3", "col6"),
] ]
@ -754,11 +754,11 @@ def test_aligned_dynamictable_ictable(intracellular_recordings_table):
rows.columns rows.columns
== pd.MultiIndex.from_tuples( == pd.MultiIndex.from_tuples(
[ [
("electrodes", "index"), ("electrodes", "id"),
("electrodes", "electrode"), ("electrodes", "electrode"),
("stimuli", "index"), ("stimuli", "id"),
("stimuli", "stimulus"), ("stimuli", "stimulus"),
("responses", "index"), ("responses", "id"),
("responses", "response"), ("responses", "response"),
] ]
) )

View file

@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item] ids = self.id[item]
if not isinstance(ids, Iterable): if not isinstance(ids, Iterable):
ids = pd.Series([ids]) ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id")) ids = pd.Index(data=ids, name="id")
tables = [ids] tables = []
for category_name, category in self._categories.items(): for category_name, category in self._categories.items():
table = category[item] table = category[item]
if isinstance(table, pd.DataFrame): if isinstance(table, pd.DataFrame):
table = table.reset_index() table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray): 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): elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index) table = pd.DataFrame({category_name: table}, index=ids)
else: else:
raise ValueError( raise ValueError(
f"Don't know how to construct category table for {category_name}" f"Don't know how to construct category table for {category_name}"
) )
tables.append(table) 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 # construct below in case we need to support array indexing in the future
else: else:
raise ValueError( raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]" "need an int, string, slice, ndarray, or tuple[int | slice, str]"
) )
df = pd.concat(tables, axis=1, keys=names) df = pd.concat(tables, axis=1, keys=self.categories)
df.set_index((self.name, "id"), drop=True, inplace=True)
return df return df
def __getattr__(self, item: str) -> Any: def __getattr__(self, item: str) -> Any: