-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Get element from series of dtypes.value_counts() by index don't work most of times #43581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
it's strange that some of those even work considering the values in the index are not strings but of type In [34]: dt.index.tolist()
Out[34]: [dtype('int64'), dtype('O'), dtype('float64')]
In [35]: dt[np.dtype('float64')]
Out[35]: 2
In [36]: dt[np.dtype('int64')]
Out[36]: 5
In [37]: dt[np.dtype('object')]
Out[37]: 5 |
@asishm So, can it be considered a problem that I tried to calculate how often it works by running this program. It turned out that either it works every time, or never. count = 0
for i in range(1000):
try:
import pandas as pd
df = pd.read_csv('titanic.csv')
dt = df.dtypes.value_counts()
dt['float64']
count += 1
except:
pass
print(count) Then I made a bash script that runs the python program and counts how often it happens. In about 47.7% of cases, #!/bin/bash
count=0
for i in {1..1000}
do
./venv/bin/python ./test.py 2> /dev/null
count=$((count + $?))
done
echo $count import pandas as pd
df = pd.read_csv('titanic.csv')
dt = df.dtypes.value_counts()
dt['float64'] |
Do you mind posting a more minimal example? Ideally the behavior should be reproducible without a CSV download so we can create a unit test if there's an issue: https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports |
@mroeschke I can't get a consistent reproducer - but run this script a few times import pandas as pd
df = pd.DataFrame([[1, 'a', 1.0]])
dt = df.dtypes.value_counts()
fail = []
dtype_list = ['float64', 'int64', 'object']
for i in dtype_list:
try:
dt[i]
except KeyError:
fail.append(i)
print(fail) output on WSL
|
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
Issue Description
I'm trying to get the number of columns of a particular data type. This code should work correctly, but most of times in crashed with KeyError on different keys. For example:
I have also tried downloading the CSV file and using dt.get() to access the values, but that also didn't work.
Expected Behavior
Expected output (approximately every 10th launch is displayed):
Installed Versions
INSTALLED VERSIONS
commit : 73c6825
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-34-generic
Version : #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : ru_RU.UTF-8
LOCALE : ru_RU.UTF-8
pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 20.0.2
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: