NJE
NJE

Reputation: 37

Convert timestamp data to date time in Python

I have a DataFrame with a column containing timestamps and I would like to convert the column to date time in Python and save the file with a column containing the date and time. Here is the code:


    import pandas as pd
    df = pd.DataFrame({
        "time": [1465585763000, 1465586363000, 1465586963000, 
                  1465587563000, 1465588163000]})
    df

Upvotes: 1

Views: 232

Answers (2)

twiddler
twiddler

Reputation: 588

This could also work

import pandas as pd
from datetime import datetime as dt
d = {'time': [1465585763000, 1465586363000, 1465586963000,
              1465587563000, 1465588163000]}


print(d['time'])
new = [dt.fromtimestamp(x/1000).strftime('%Y-%m-%d %H:%M:%S') for x in d['time']]
pd.to_datetime(new)


Upvotes: 1

Ashok Kumar
Ashok Kumar

Reputation: 89

This could work

from datetime import datetime as dt
import pandas as pd

times = [
    1465585763000, 
    1465586363000, 
    1465586963000, 
    1465587563000, 
    1465588163000]

start_ts = dt.timestamp(dt(1970, 1, 1))
dates = [dt.fromtimestamp(time / 1000 + start_ts) for time in times]
pd.to_datetime(dates)

Upvotes: 0

Related Questions