Last active
July 26, 2023 11:04
-
-
Save jupihes/98ae06d2e65eba5cd73d057de95be8c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import jdatetime | |
from pandas import to_datetime | |
#def p_to_g(d, splitter='/'): | |
# ''' | |
# Persian date instance to Gregorian date instance converstion assuming input date format to be format="%Y/%m/%d" | |
# and provide output in same format. | |
# ''' | |
# d = d.split(splitter) | |
# | |
# return to_datetime(jdatetime.date(int(d[0]), int(d[1]),int(d[2])).togregorian(),\#utc=True, | |
# format="%Y/%m/%d") | |
def p_to_g(d, splitter='/', date_out_format="%Y/%m/%d"): | |
# # d = jdatetime.datetime.now(d).strftime("%Y/%m/%d").split('/') | |
d = d.split(splitter) | |
# # pd.to_datetime(jdatetime.date(int(d[0]), int(d[1]),int(d[2])).togregorian(),format="%Y/%m/%d") | |
# # jdatetime.date(int(d[0]), int(d[1]),int(d[2])).togregorian() | |
return jdatetime.date(int(d[0]), int(d[1]),int(d[2])).togregorian().strftime(date_out_format) | |
############ | |
sample_date = '1330/12/09' | |
gdate = p_to_g(sample_date) | |
Out[1]: Timestamp('1952-02-29 00:00:00+0000') | |
gdate.date() | |
Out[2]: datetime.date(1952, 2, 29) | |
p_to_g(sample_date, splitter='/',date_out_format = '%y-%M-%d') | |
Out[3]: '52-00-29' | |
sample_date1 = '1330-12-09' | |
p_to_g(sample_date1,splitter='-') | |
Out[4]: '1952/02/29' | |
############ very clean functions | |
def p_to_g(d, splitter='/', date_out_format="%Y/%m/%d"): | |
''' p_to_g('1401/06/31') '2022/09/22''' | |
import jdatetime #from jdatetime.date import fromgregorian, togregorian | |
d = d.split(splitter) | |
return jdatetime.date(int(d[0]), int(d[1]),int(d[2])).\ | |
togregorian().strftime(date_out_format) | |
def g_to_p(d, splitter='/', date_out_format="%Y/%m/%d"): | |
''' g_to_p('1900/01/01') '1278/10/11' ''' | |
import jdatetime | |
d = d.split(splitter) | |
return jdatetime.date.fromgregorian(day=int(d[2]), | |
month=int(d[1]), | |
year=int(d[0])).\ | |
strftime(date_out_format) | |
############ | |
# apply to dataframe columns | |
df['gdate'] = df.apply(lambda x: p_to_g(x['pdate']), axis=1) | |
############ | |
# p_to_g to be added |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment