简体   繁体   中英

Python Pandas convert date to epoch timestamp

From CSV file, i'm trying with pandas to convert a date column to epoch timestamp as follow, but i got some errors:

csv:

<<Electric power and temperature Information>>
Date,Electric power average,Electric power maximum value,Electric power minimum value,...,...
2021/12/02 00:00:00,1524,1553,1506,22,22,22,,,,,,,21,21,21,,,,,,,,,,,,,,,,,,,,,,,,
2021/12/01 22:00:00,1521,1547,1468,22,22,22,,,,,,,21,21,21,,,,,,,,,,,,,,,,,,,,,,,,
2021/12/01 20:00:00,1546,1613,1524,22,22,22,,,,,,,21,21,21,,,,,,,,,,,,,,,,,,,,,,,,
2021/12/01 18:00:00,1553,1595,1525,22,22,22,,,,,,,21,21,21,,,,,,,,,,,,,,,,,,,,,,,,
2021/12/01 16:00:00,1541,1593,1520,22,22,22,,,,,,,21,21,21,,,,,,,,,,,,,,,,,,,,,,,,
2021/12/01 14:00:00,1540,1580,1514,22,22,22,,,,,,,21,21,21,,,,,,,,,,,,,,,,,,,,,,,,

code:

  csv_envfile = csvfile.csv
   df = pd.read_csv(csv_envfile[0], skiprows=[0])
   date_pattern='%Y/%m/%d %H:%M:%S '
   df['epoch'] = df.apply(lambda row: int(time.mktime(time.strptime(row.time,date_pattern))), axis=0) # create epoch as a column
   print("epoch:",df['epoch'])

error:

Traceback (most recent call last):
  File "./02-pickle-client.py", line 622, in <module>
    main()
  File "./02-pickle-client.py", line 576, in main
    execute_run_csv_environnement(confcsv_path, storage_type, serial)
  File "./02-pickle-client.py", line 434, in execute_run_csv_environnement
    run_csv_environnement(sock, delay, csvfile, storage_type, serial)
  File "./02-pickle-client.py", line 402, in run_csv_environnement
    df['epoch'] = df.apply(lambda row: int(time.mktime(time.strptime(row.time,date_pattern))), axis=0) # create epoch as a column
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/frame.py", line 7552, in apply
    return op.get_result()
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/apply.py", line 185, in get_result
    return self.apply_standard()
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/apply.py", line 276, in apply_standard
    results, res_index = self.apply_series_generator()
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/apply.py", line 305, in apply_series_generator
    results[i] = self.f(v)
  File "./02-pickle-client.py", line 402, in <lambda>
    df['epoch'] = df.apply(lambda row: int(time.mktime(time.strptime(row.time,date_pattern))), axis=0) # create epoch as a column
  File "/usr/local/lib64/python3.6/site-packages/pandas/core/generic.py", line 5141, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'time'

Many thanks for help

You should select the Date column when applying the lambda function. In your case this should work:

import pandas as pd
import time

csv_envfile = csvfile.csv
df = pd.read_csv(csv_envfile[0], skiprows=[0])
date_pattern='%Y/%m/%d %H:%M:%S'
df['epoch'] = df["Date"].apply(lambda row: int(time.mktime(time.strptime(row,date_pattern))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM