0

What is the correct way to read time series data in csv format which is sent to AWS Lambda through API Getaway?

import pandas as pd

def lambda_handler(event, context)

    data = pd.read_csv(event['body'], index_col='time', parse_dates=['time'])

I tried to use read_csv function to parse event body, but getting an error.

{
  "errorMessage": "'body'",
  "errorType": "KeyError",
  "requestId": "d7759f9e-4ef6-4ffa-bc9c-bf9379b47d58",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 6, in lambda_handler\n    v = event['body'].read()\n"
  ]
}
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Might be useful: [amazon web services - Getting json body in aws Lambda via API gateway - Stack Overflow](https://stackoverflow.com/questions/41648467/getting-json-body-in-aws-lambda-via-api-gateway) – John Rotenstein Sep 09 '21 at 22:06

1 Answers1

0

I figured out the way it can be done. I used StringIO function from io library.

import pandas as pd
import io

def lambda_handler(event, context):

    data = pd.read_csv(io.StringIO(event['body']), index_col='time', parse_dates=['time'])