Skip to content

Commit

Permalink
Merge branch 'main' into ig/refactor_base_class
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-gold authored Jun 6, 2023
2 parents 574f0f5 + e067515 commit 81b7d58
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.265'
rev: 'v0.0.270'
hooks:
- id: ruff
args: ["--fix"]
12 changes: 10 additions & 2 deletions anndata/_core/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ def _check_2d_shape(X):
@singledispatch
def _gen_dataframe(anno, length, index_names):
if anno is None or len(anno) == 0:
return pd.DataFrame({}, index=pd.RangeIndex(0, length, name=None).astype(str))
anno = {}
for index_name in index_names:
if index_name in anno:
return pd.DataFrame(
anno,
index=anno[index_name],
columns=[k for k in anno.keys() if k != index_name],
)
return pd.DataFrame(anno, index=pd.RangeIndex(0, length, name=None).astype(str))
return pd.DataFrame(
anno,
index=pd.RangeIndex(0, length, name=None).astype(str),
columns=None if len(anno) else [],
)


@_gen_dataframe.register(pd.DataFrame)
Expand All @@ -117,6 +121,8 @@ def _(anno, length, index_names):
if not is_string_dtype(anno.index):
warnings.warn("Transforming to str index.", ImplicitModificationWarning)
anno.index = anno.index.astype(str)
if not len(anno.columns):
anno.columns = anno.columns.astype(str)
return anno


Expand Down Expand Up @@ -764,6 +770,8 @@ def _set_dim_df(self, value: pd.DataFrame, attr: str):
self._init_as_actual(self.copy())
setattr(self, f"_{attr}", value)
self._set_dim_index(value_idx, attr)
if not len(value.columns):
value.columns = value.columns.astype(str)

def _prep_dim_index(self, value, attr: str) -> pd.Index:
"""Prepares index to be uses as obs_names or var_names for AnnData object.AssertionError
Expand Down
6 changes: 3 additions & 3 deletions anndata/_io/specs/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def read_dataframe(elem, _reader):
df = pd.DataFrame(
{k: _reader.read_elem(elem[k]) for k in columns},
index=_reader.read_elem(elem[idx_key]),
columns=list(columns),
columns=columns if len(columns) else None,
)
if idx_key != "_index":
df.index.name = idx_key
Expand All @@ -612,7 +612,7 @@ def read_dataframe_partial(
df = pd.DataFrame(
{k: read_elem_partial(elem[k], indices=indices[0]) for k in columns},
index=read_elem_partial(elem[idx_key], indices=indices[0]),
columns=list(columns),
columns=columns if len(columns) else None,
)
if idx_key != "_index":
df.index.name = idx_key
Expand All @@ -630,7 +630,7 @@ def read_dataframe_0_1_0(elem, _reader):
df = pd.DataFrame(
{k: read_series(elem[k]) for k in columns},
index=read_series(elem[idx_key]),
columns=list(columns),
columns=columns if len(columns) else None,
)
if idx_key != "_index":
df.index.name = idx_key
Expand Down
8 changes: 3 additions & 5 deletions anndata/tests/test_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,11 +1086,9 @@ def test_concatenate_uns(unss, merge_strategy, result, value_gen):
print(merge_strategy, "\n", unss, "\n", result)
result, *unss = permute_nested_values([result] + unss, value_gen)
adatas = [uns_ad(uns) for uns in unss]
assert_equal(
adatas[0].concatenate(adatas[1:], uns_merge=merge_strategy).uns,
result,
elem_name="uns",
)
with pytest.warns(FutureWarning, match=r"concatenate method is deprecated"):
merged = AnnData.concatenate(*adatas, uns_merge=merge_strategy).uns
assert_equal(merged, result, elem_name="uns")


def test_transposed_concat(array_type, axis, join_type, merge_strategy, fill_val):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ omit = [
[tool.pytest.ini_options]
addopts = "--doctest-modules"
python_files = "test_*.py"
testpaths = ["anndata", "docs/concatenation.rst"]
testpaths = ["anndata/tests", "docs/concatenation.rst"]
filterwarnings = [
'ignore:X\.dtype being converted to np.float32:FutureWarning'
]
Expand Down

0 comments on commit 81b7d58

Please sign in to comment.