Skip to content

Commit

Permalink
feat: add convert data to pandas function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mel-liow committed Jan 18, 2022
1 parent f2c5658 commit 81395c4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/airpyllution/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from datetime import datetime
import pandas as pd

def convert_unix_to_date(utime):
"""Returns formatted date time string
Expand All @@ -20,3 +21,25 @@ def convert_unix_to_date(utime):
"""
ts = int(utime)
return datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')


def convert_data_to_pandas(raw_data):
"""Converts API data from OpenWeatherMap to a pandas dataframe
Parses the JSON data object and transforms it to a dataframe with a formatted
date column
Parameters
----------
raw_data : JSON object
Returns
-------
Pandas.dataframe
Examples
--------
>>> convert_data_to_pandas(data)
"""
data = pd.DataFrame.from_records(list(map(lambda x:x["components"],raw_data["list"])))
data["dt"] = list(map(lambda x:convert_unix_to_date(x["dt"]),raw_data["list"]))
return data

0 comments on commit 81395c4

Please sign in to comment.