Convert timestamp to dd / mm / yyyy and add Timezone to Dataframe Panda

0

I am loading information into a DataFrame of the Python Panda module, where I have a timestamp type record, that information is brought from MongoDb

cursor = collection.find( { "sono" : str(sono), "time" :  { "$gt" : int(start) }, "time" :{"$lt": int(end) }}, {"time" : 1, "timezone" : 1, "_id" :0 })
df = pd.DataFrame(list(cursor),columns=['time','timezone'])

Then I load the information in the DataFrame:

df = pd.DataFrame(list(cursor),columns=['time','timezone'])

Then I convert the data type timestamp to datetime:

df['time'] = pd.to_datetime(df['time'], unit='s')

Now the problem is I need to add the timezone column to the time

The information that I display is:

                     time  timezone
0     2018-08-14 15:31:50        +3
1     2018-08-14 15:31:51        +3
2     2018-08-14 15:31:52        +3
3     2018-08-14 15:31:53        +3
4     2018-08-14 15:31:54        +3
5     2018-08-14 15:31:55        +3

It should look like this:

                     time  timezone
0     2018-08-14 18:31:50        +3
1     2018-08-14 18:31:51        +3
2     2018-08-14 18:31:52        +3
3     2018-08-14 18:31:53        +3
4     2018-08-14 18:31:54        +3
5     2018-08-14 18:31:55        +3
    
asked by Alejandro 16.08.2018 в 21:36
source

1 answer

1

You can simply use pandas.to_timedelta on the timezone column % specifying the unit (hours).

First an example that can be reproduced:

import pandas as pd

data = {"time": ("2018-08-23 01:31:50",
                 "2018-08-14 23:31:51",
                 "2018-08-14 12:31:52",
                 "2018-08-14 15:31:53",
                 "2018-08-14 22:31:54",
                 "2018-08-14 17:31:55"),
        "timezone": (-2, 1, -1, -3, 2, 3)
        }


df = pd.DataFrame(data)
df["time"] = pd.to_datetime(df["time"])

With this we can already do something:

>>> df
                 time  timezone
0 2018-08-23 01:31:50        -2
1 2018-08-14 23:31:51         1
2 2018-08-14 12:31:52        -1
3 2018-08-14 15:31:53        -3
4 2018-08-14 22:31:54         2
5 2018-08-14 17:31:55         3

>>> df["time"] += pd.to_timedelta(df.timezone, unit="h")

>>> df
                 time  timezone
0 2018-08-22 23:31:50        -2
1 2018-08-15 00:31:51         1
2 2018-08-14 11:31:52        -1
3 2018-08-14 12:31:53        -3
4 2018-08-15 00:31:54         2
5 2018-08-14 20:31:55         3
    
answered by 16.08.2018 в 22:31