0

This is a part of my DataFrame

          0                   1
0 -0.045988 2018-01-01 00:00:00
1 -0.046024 2018-01-01 00:01:00
2 -0.044360 2018-01-01 00:02:00
3 -0.044722 2018-01-01 00:03:00
4 -0.043637 2018-01-01 00:04:00

for example, I want to find the -0.043637 in DataFrame:

X.isin([-0.043637])

but the result is like

       0      1
0  False  False
1  False  False
2  False  False
3  False  False
4  False  False

The type of DataFrame is

0           float64
1    datetime64[ns]
dtype: object

It is really strang that it does not work and return the wrong answer. What's your idea, why it happened?

  • 1
    Float point precision, `-0.043637` is what printed, might not be what stored in memory. For float value lookup, you should use some sort of tolerance. – Quang Hoang Feb 24 '20 at 17:35
  • Or you could work with strings `df.astype(str).eq('-0.043637')` – yatu Feb 24 '20 at 17:39
  • Does this answer your question? [Can I use pandas.dataframe.isin() with a numeric tolerance parameter?](https://stackoverflow.com/questions/39602004/can-i-use-pandas-dataframe-isin-with-a-numeric-tolerance-parameter) – AMC Feb 24 '20 at 17:48

1 Answers1

1

Comparing float is complicated and to simplify, you have to provide precision.

so, instead your code can be written as,

df[0].apply(lambda x: np.isclose(x, -0.043637, atol=1e-5))

For performance, you can convert the float to str when comparing,

df[0].astype(str).apply(lambda x: x.startswith("-0.043637"))
DOOM
  • 1,170
  • 6
  • 20