Ant
Ant

Reputation: 1153

Trying to Modify a Script to Take Timestamp Date Format Instead of %Y-%m-%d Format [Python]

Pulling dates from a csv in Timestamp format (ex. 1504159200).
I'm plugging these into a script suited for Y-m-d format.

[The code is a massive LSTM script so hopefully the original lines and what I tried on them will suffice]

startDate = 1504159200  
endDate = 1519974000

Original lines without modifying...

sd = datetime.strptime(startDate, '%Y-%m-%d')
ed = datetime.strptime(endDate, '%Y-%m-%d')

Traceback (most recent call last):
File "stocks.py", line 91, in <module>
p, n = Main(['last_vix_92_18.csv', '1504159200', '1519974000', 'D'])
File "stocks.py", line 78, in Main
P = sp.PredictDate(args[1], args[2], predPrd)
File "/home/eagle/predict/dragon/pythonml-master/Stocks/StockPredictor.py", line 282, in PredictDate
ts = DateRange(startDate, endDate, period)[::-1]
File "/home/eagle/predict/dragon/pythonml-master/Stocks/StockPredictor.py", line 30, in DateRange
sd = datetime.strptime(startDate, '%Y-%m-%d')
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '1504159200' does not match format '%Y-%m-%d'

I've tried modifying the code thusly with the following error below.

sd = datetime(int(startDate)).timestamp()
ed = datetime(int(endDate)).timestamp()  

Traceback (most recent call last):
File "stocks.py", line 91, in <module>
p, n = Main(['last_vix_92_18.csv', '1504159200', '1519974000', 'D'])
File "stocks.py", line 78, in Main
P = sp.PredictDate(args[1], args[2], predPrd)
File "/home/eagle/predict/dragon/pythonml-master/Stocks/StockPredictor.py", line 284, in PredictDate
ts = DateRange(startDate, endDate, period)[::-1]
File "/home/eagle/predict/dragon/pythonml-master/Stocks/StockPredictor.py", line 31, in DateRange
sd = datetime(int(startDate)).timestamp()
TypeError: Required argument 'month' (pos 2) not found

Upvotes: 1

Views: 86

Answers (1)

Alex
Alex

Reputation: 19114

Just use datetime.datetime.fromtimestamp:

from datetime import datetime

datetime.fromtimestamp(1504159200)  # datetime.datetime(2017, 8, 31, 2, 0)
datetime.fromtimestamp(1519974000)  # datetime.datetime(2018, 3, 2, 2, 0)

Upvotes: 3

Related Questions