Map function python error

Hello,

I’m trying to convert _time column into int (or uint) while querying through python client library but I’m getting the error

Traceback (most recent call last):
  File "/home/ubuntu/python/push/push.py", line 62, in <module>
    run()
  File "/home/ubuntu/python/push/push.py", line 57, in run
    read_tables()
  File "/home/ubuntu/python/push/push.py", line 44, in read_tables
    tables = client.query_api().query('''import "strings"
KeyError: 'r with _time'

The piece of code is:

def read_tables():
    tables = client.query_api().query('''import "strings"
                                         from(bucket:"test")
                                         |> range(start: -{window_str}s)
                                         |> filter(fn: (r) => strings.hasPrefix(v: r._measurement, prefix: "WO"))
                                         |> map(fn: (r) => ({r with _time: int(v: r._time)}))
                                         |> drop(columns: ["_start", "_stop"])'''
                                         .format(window_str = window))

What I’m I doing wrong?

Thank you!

@fgarcia It looks like this error is coming from the client and not from Flux itself. I’m guessing that the Python client expects the _time column to be a time type. Try mapping the int/uint version of the timestamp to a different column.

def read_tables():
    tables = client.query_api().query('''import "strings"
                                         from(bucket:"test")
                                         |> range(start: -{window_str}s)
                                         |> filter(fn: (r) => strings.hasPrefix(v: r._measurement, prefix: "WO"))
                                         |> map(fn: (r) => ({r with time_int: int(v: r._time)}))
                                         |> drop(columns: ["_start", "_stop"])'''
                                         .format(window_str = window))

Hi Scott,

Thank you for your help, unfortunately it did not work either.

I get the same error even if I try with “_value” column

def read_tables():
    tables = client.query_api().query('''import "strings"
                                         from(bucket:"test")
                                         |> range(start: -{window_str}s)
                                         |> filter(fn: (r) => strings.hasPrefix(v: r._measurement, prefix: "WO"))
                                         |> map(fn: (r) => ({r with _value: int(v: r._value)}))
                                         |> drop(columns: ["_start", "_stop"])'''
                                         .format(window_str = window))

My bad!

Python code should be:

def read_tables():
    tables = client.query_api().query('''import "strings"
                                         from(bucket:"test")
                                         |> range(start: -{window_str}s)
                                         |> filter(fn: (r) => strings.hasPrefix(v: r._measurement, prefix: "WO"))
                                         |> map(fn: (r) => ({{r with _time: int(v: r._time)}}))
                                         |> drop(columns: ["_start", "_stop"])'''
                                         .format(window_str = window))

Double curly braces…

Thank you again!