Decipher date serial number in Yahoo Finance - python

I need help deciphering the date number at the end of this url: view-source:http://finance.yahoo.com/q/op?s=XOM&date=1434672000 -- it doesn't look like a proleptic Gregorian serial number, but in Yahoo it designates June 19, 2015. My goal is to write a Python code segment that will create a valid Yahoo date number from my input of yyyymmdd, so that I can generate a valid url for any stock ticker symbol (not just XOM) and option expiration date expressed as yyyymmdd. Thanks!

That's a UNIX timestamp -- the number of seconds since January 1, 1970.
>>> time.gmtime(1434672000)
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=170, tm_isdst=0)

You can use datetime.fromtimestamp to convert the timestamp into a datetime object:
from datetime import datetime
url="http://finance.yahoo.com/q/op?s=XOM&date=1434672000"
print(datetime.fromtimestamp(float(url.rsplit("=",1)[1])))
2015-06-19 01:00:00
print(datetime.fromtimestamp(float(url.rsplit("=",1)[1])).date())
2015-06-19
To create a timestamp using a date string use strptime to create a datetime object and then call .timestamp():
dte = "2015-06-19"
print(datetime.strptime(dte,"%Y-%m-%d").timestamp())
Using urllib.parse is probably a nicer way to extract the date:
from datetime import datetime
url="http://finance.yahoo.com/q/op?s=XOM&date=1434672000"
from urllib.parse import parse_qs
print(datetime.fromtimestamp(float(parse_qs(url)["date"][0])))

Thank you; now I know how to go both ways with the timestamp (deciphering it, and creating it). Regarding creating it, I found out about the calendar module when I discovered the following code for creating a timestamp from a ddmmmyyyy string (I tested this code in the shell):
#----------------- Create time stamp
import time
import datetime
import calendar
# oxdt stands for 'option expiration date'
oxdt_txt = '15may2015'
oxdt = datetime.datetime.strptime(oxdt_txt, '%d%b%Y')
print(oxdt_txt)
print(oxdt)
print(calendar.timegm(oxdt.utctimetuple()))
print()
oxdt_txt = '19jun2015'
oxdt = datetime.datetime.strptime(oxdt_txt, '%d%b%Y')
print(oxdt_txt)
print(oxdt)
print(calendar.timegm(oxdt.utctimetuple()))
#----------------- done

Related

Convert Shell date format to Python date format

Can below piece of shell date format be converted to python date format?
date_used = $(date --date="$(date +%Y-%m-15) - 1 month" "+%Y_%m")
As per my understanding this above format is just taking day as 15 of current month and it simply subtracts 1 month and results in giving output in the format of year_month.
Output of the above shell date format -->
echo $date_used = 2022_05
Can this particular scenario be done using python?
Any update about it would be really appreciable.
An equivalent would be:
from datetime import datetime,timedelta
# Current date, replace date with 15, subtract 30 days and format
date_used = (datetime.now().replace(day=15) - timedelta(days=30)).strftime('%Y_%m')
print(date_used)
Output:
2022_05
You can use python's datetime module to do this
it has a function named strptime you can use to read date and time data with format code very similar to unix date format (or maybe its even same i'm not sure)
and strftime to output in a certain format as well
you can see the functions and the format code to see if there's any different on
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
Example code
from datetime import datetime, timedelta
date = datetime.strptime((datetime.now() - timedelta(days=30)).strftime("%Y-%m") + "-15", "%Y-%m-%d")
print(date)
print(date.strftime("%Y_%m"))
output
2022-05-15 00:00:00
2022_05

Python datetime to Excel serial date conversion

The following code converts a string into a timestamp. The timestamp comes out to: 1646810127.
However, if I use Excel to convert this date and time into a float I get: 44629,34.
I need the Excel's output from the Python script.
I have tried with a few different datetime strings to see if there is any pattern in between the two numbers, but cannot seem to find any.
Any thoughts on how I get the code to output 44629,34?
Much appreciated
import datetime
date_time_str = '2022-03-09 08:15:27'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
print(date_time_obj.timestamp())
>>output:
Date: 2022-03-09
Time: 08:15:27
Date-time: 2022-03-09 08:15:27
1646810127.0
calculate the timedelta of your datetime object versus Excel's "day zero", then divide the total_seconds of the timedelta by the seconds in a day to get Excel serial date:
import datetime
date_time_str = '2022-03-09 08:15:27'
UTC = datetime.timezone.utc
dt_obj = datetime.datetime.fromisoformat(date_time_str).replace(tzinfo=UTC)
day_zero = datetime.datetime(1899,12,30, tzinfo=UTC)
excel_serial_date = (dt_obj-day_zero).total_seconds()/86400
print(excel_serial_date)
# 44629.3440625
Note: I'm setting time zone to UTC here to avoid any ambiguities - adjust as needed.
Since the question is tagged pandas, you'd do the same thing here, only that you don't need to set UTC as pandas assumes UTC by default for naive datetime:
import pandas as pd
ts = pd.Timestamp('2022-03-09 08:15:27')
excel_serial_date = (ts-pd.Timestamp('1899-12-30')).total_seconds()/86400
print(excel_serial_date)
# 44629.3440625
See also:
background: What is story behind December 30, 1899 as base date?
inverse operation: Convert Excel style date with pandas

how to convert time and time zone into date in python?

One column of CSV file includes time and time zone.
Here is one value under the column: 2018-05-20 15:05:51.065 America/New_York. I wonder, how can I convert the value to the 2019-05-20 format? There are over a half-million rows in the CSV file.
Split your column into date, time and zone using string manipulators, regex etc . Have a standard time zone to follow (eg: UTC)
Now
Get time difference between the zone and UTC using below,
How to convert string timezones in form (Country/city) into datetime.tzinfo
Use this difference to the time you have split already and then change date based on 24 hours.
If you just want it to be a string, just strip away everything past the first space:
"2018-05-20 15:05:51.065 America/New_York".split(' ')[0]
EDIT:
If you want it to be a timezone-aware datetime object, you can do it easily with pytz package:
from datetime import datetime
from pytz import timezone
string_date = "2018-05-20 15:05:51.065 America/New_York"
tz = timezone(string_date.split(' ')[len(string_date.split(' '))-1])
unaware = " ".join(string_date.split(' ')[:len(string_date.split(' '))-1])
unaware_datetime = datetime.strptime(unaware, "%Y-%m-%d %H:%M:%S.%f")
aware_datetime = unaware_datetime.replace(tzinfo=tz)

datetime, adding a day and getting correct format

There are various ways of adding a day for example:
import datetime
print(datetime.datetime.now() + datetime.timedelta(days=1))
The problem with this is that the answer is: 2016-12-06 16:52:44.679431
I only need 2016-12-06. I can easily get that by performing a string manipulation like splitting. I as wondering if there was a way to do it directly.
secondly:
From what I have read from the documentation the below two ways should give me the time in my timezone neither do though.
import time
print(time.localtime())
result: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=5, tm_hour=17, tm_min=50, tm_sec=56, tm_wday=0, tm_yday=340, tm_isdst=0)
&
import datetime
print(datetime.datetime.now())
return 2016-12-05 17:52:09.045170
neither do, they both give me GMT:
How do I get my local timezone?
Summary:
Is there a direct way to a day and get the correct form? an
How do I get my local timezone?
According to the documentation:
datetime.date()
Return date object with same year, month and day.
In your case:
import datetime
print("local hour: "+str((datetime.datetime.now() + datetime.timedelta(days=1)).date()))
print("utc hour: "+str((datetime.datetime.utcnow() + datetime.timedelta(days=1)).date()))
Output:
local hour: 2016-12-06
utc hour: 2016-12-06
Another way is change datetime.datetime.today() to datetime.date.today():
import datetime
print(datetime.date.today() + datetime.timedelta(days=1))
Output:
2016-12-06

python datetime and date comparison

Based on the example I found here I want to grab icalendar data and process it. This my code so far:
from datetime import datetime, timedelta
from icalendar import Calendar
import urllib
import time
ics = urllib.urlopen('https://www.google.com/calendar/ical/xy/basic.ics').read()
ical1 = Calendar.from_ical(ics)
for vevent in ical1.subcomponents:
if vevent.name == "VEVENT":
title = str(vevent.get('SUMMARY').encode('utf-8'))
start = vevent.get('DTSTART').dt # datetime
end = vevent.get('DTEND').dt # datetime
print title
print start
print end
print type(start)
print "---"
It is fetching the title and start+end date from my google calender. This working and the output looks like this:
This is a Test Title
2012-12-20 15:00:00+00:00
2012-12-20 18:00:00+00:00
<type 'datetime.datetime'>
---
Another Title
2012-12-10
2012-12-11
<type 'datetime.date'>
---
...
As you can see the start and end date can be of type datetime.datetime or datetime.date. It depends on whether I have an entry in google calender for 2 whole days (then it is a datetime.date) or for a time period e.g. 3 hours (then it is a datetime.datetime. I need to only print dates/datetimes from today and for the next 4 weeks. I had problems comparing dates and datetimes (its seems like it is not possible) so I failed to do that. How can I compare datetimes and dates conserving the data types? If I need to "cast" them, its ok. I was not able to print "today + 4 weeks", only "4 weeks".
You will need to convert to a common type for comparison. Since both datetime's and date's have dates in common, it makes sense to convert -> date types. Just extract the date() from the datetime. For example:
>>> import datetime
>>> datetime.date(2014, 2, 22) == datetime.datetime.now().date()
True
Datetime objects can return an appropriate date object by calling date on them: start.date() which then can be compared to another date object: start.date() < datetime.date.today() + datetime.timedelta(4*7)

Categories

Resources