1

Good day

I have a pandas dataframe with 3 columns. Column two is in date format yyyy-mm-dd but I would like to convert these to ticks. I would like to know the most efficient way to do this (i.e. I don't want to loop through each row).

My code is as follows:

#Example of getting ticks
import time;  # This is required to include time module.

ticks = time.time()
print("Number of ticks since 12:00am, January 1, 1970:", ticks)

#Example of converting to ticks

import datetime
s = "01/12/2015"
time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())

#My code

import numpy as np
import pandas as pd

path = "C:/Users/geoff/OneDrive - Noble Brothers Consult (Pty) Ltd/Clients/GrAM/Daily Price Data/"
filename = "SST_5_Sept_2016_formatted.csv"
full_path = path + filename

data = pd.read_csv(full_path)

#here i need to convert the column data['Date'] to ticks

I have uploaded a screenshot my datafile as well:

Screenshot of datafile:

screenshot

Thanks very much for your help.

svrist
  • 7,042
  • 7
  • 44
  • 67
gfro_za
  • 23
  • 3

1 Answers1

0

First, wrap the converting-to-ticks part in a function, such as

def convert_to_tic(s):
    return time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())

Then, turn your column into a DataFrame and use apply. Something in the likes of what is suggested in the bottom part of this answer

Community
  • 1
  • 1
lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48