Trying to convert an Int64Index to an Int

Hello! I am trying to refer to an index value as an integer. Does anyone know how to do this?

index_value = data.index[data.timestamp == int(slice_end_timestamp)]

My desired output is ‘220289’
Current output is: ‘Int64Index([220289], dtype=‘int64’)’

Thank you in advance.

Hi Adam,

We have to guess, because we don’t know what data is, or where Int64Index comes from. Is it maybe something from pandas or numpy?

But try this:

indices = data.index[data.timestamp == int(slice_end_timestamp)]
index_value = indices[0]

If that second line fails, try this:

index_value = indices.to_list()[0]

As a single line:

index_value = data.index[data.timestamp == int(slice_end_timestamp)][0]

or if necessary:

index_value = data.index[data.timestamp == int(slice_end_timestamp)].to_list()[0]

If that still fails, then I have guessed wrong and we’ll need more information in order to solve your problem.

Start by telling us the version of Python you are using, the operating system (Linux, Mac OS, Windows, something else?) and the type of index_value:

print(repr(index_value))
print(type(index_value))
1 Like

By Steven D’Aprano via Discussions on Python.org at 26Jun2022 01:51:

We have to guess, because we don’t know what data is, or where
Int64Index comes from. Is it maybe something from pandas or numpy?

It’s probably this:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Int64Index.html

I think a lot of students are using environments with pandas
preimported, often as pd, and don’t really think that these might be
specialist names.

Cheers,
Cameron Simpson cs@cskk.id.au

Steven, thanks for your help. your first line worked for me. I am using pandas by the way.

Thanks so much, Steven, for the help. It worked when I used this