Write_api.write(): Exception: cannot do slice indexing on DatetimeIndex with these indexers [] of type str

I am facing an issue while trying to write into InfluxDB 2.7 with python library influxdb-client==1.48.0.

I have created a pandas DataFrame with this schema:

signalname float64
imo object
dtype: object

Here is a df.head() example:

_time signalname tag_number
1744784582000000000 1.01591 8842250
1744784589000000000 1.01550 8842250
1744784599000000000 1.01580 8842250
1744784609000000000 1.01560 8842250
1744784619000000000 1.01550 8842250

_time column is the index of the DF and it’s in Nanoseconds.

Here is the code snippet I use:

tags = ['tag_number']
source = 'source1'
with InfluxDBClient(
    url=influx_url,
    token=influx_token,
    org=influx_org
) as client:
    write_api = client.write_api(
        write_options=WriteOptions(batch_size=influx_batch_size)
    )
    write_api.write(
        influx_bucket,
        influx_org,
        record=df,
        data_frame_tag_columns=tags,
        data_frame_measurement_name=source,
    )
    write_api.close()

On every call to write_api.write(…) I get the following exception:

cannot do slice indexing on DatetimeIndex with these indexers [] of type str

Can somebody help?

Can you double check if your DataFrame index is a real datetime index (not strings) and that tag_number is an actual column (not the index), also check for hidden whitespace in column names before calling write_api.write()?

Moreover, here is an helpful article that shows how to turn time column into a pandas DatetimeIndex

In fact it is an integer. I have been ingesting data in influxdb 2.7.4 using nanosecond epochs integers for long. Thus, I don’t understand the exception

I see - the timecolumn, looking into it further.

1 Like

I finally found the issue.

I was reading the influx_batch_size in my cloud dev environment as a str.
write_api = client.write_api(
write_options=WriteOptions(batch_size=influx_batch_size)
)

When creating the write_api, there was no exception called.

Only when write_api.write() function is called the exception is raised. But it is not even an influx-client exception, it is a pandas exception.
Despite being a silly error, I believe this typing issues should be controlled by the influx-dbclient libraries, and raise an appropriate informative exception.

1 Like

Unfortunately out 1.x client is no longer actively maintained but glad you found and fixed the issue.