Skip to content

BUG: pd.Index with pd.array does not respect dtype. #45206

Closed
@DamianBarabonkovQC

Description

@DamianBarabonkovQC

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

python
import pandas as pd

# Buggy behavior
print(pd.Index(pd.array([1,0]), dtype='object'))
# Index([1, 0], dtype='Int64')

# Correct behavior
print(pd.Index([1,0], dtype='object'))
# Index([1, 0], dtype='object')

Issue Description

This issue affects the latest version: '1.4.0.dev0+1574.g46ddb8ef88'

In older versions, such as 1.3.5, have the first argument as a pandas array does not affect the output dtype. With the latest version, this is not the case. The output dtype should be the dtype requested in the argument, in this case 'object'.

Expected Behavior

import pandas as pd

Buggy behavior

print(pd.Index(pd.array([1,0]), dtype='object'))
# Index([1, 0], dtype='object')

Installed Versions

pd.show_versions()
/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/_distutils_hack/init.py:24: UserWarning: Distutils was imported before Setuptools, but importing Setuptools also replaces the distutils module in sys.modules. This may lead to undesirable behaviors or errors. To avoid these issues, avoid using distutils directly, ensure that setuptools is installed in the traditional way (e.g. not an editable install), and/or make sure that setuptools is always imported before distutils.
warnings.warn(
/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/_distutils_hack/init.py:36: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
Traceback (most recent call last):
File "", line 1, in
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/pandas/util/_print_versions.py", line 109, in show_versions
deps = _get_dependency_info()
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/pandas/util/_print_versions.py", line 88, in _get_dependency_info
mod = import_optional_dependency(modname, errors="ignore")
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/pandas/compat/_optional.py", line 116, in import_optional_dependency
module = importlib.import_module(name)
File "/opt/miniconda3/envs/nightly/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 883, in exec_module
File "", line 241, in _call_with_frames_removed
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/setuptools/init.py", line 8, in
import _distutils_hack.override # noqa: F401
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/_distutils_hack/override.py", line 1, in
import('_distutils_hack').do_override()
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/_distutils_hack/init.py", line 73, in do_override
ensure_local_distutils()
File "/opt/miniconda3/envs/nightly/lib/python3.10/site-packages/_distutils_hack/init.py", line 61, in ensure_local_distutils
assert '_distutils' in core.file, core.file
AssertionError: /opt/miniconda3/envs/nightly/lib/python3.10/distutils/core.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions