so I'm trying to figure out a way to convert a normal date in the format "dd/mm/yyyy" (for example : 31/12/2016).
I want to find a way to convert this date into a unique number, so I can re-convert it back.
for example i thought of sum=day+month*12 + year*365 as the number and then :
(sum % 365 ) / 12...
but it's not working for each statment. so any ideas?
It is far better not to handle the strings yourself. Instead use the module called datetime. Here is an incredibly good answer which should hopefully satisfy what you need to do. Converting string into datetime
For example, in your case you would need the following
import datetime
your_date = datetime.datetime.strptime("31/12/2016", "%d/%m/%Y")
Then this article How to convert datetime to integer in python explains how you can turn it into an integer, but as stated in the answer, this is usually a bad idea.
You can use datetime module to extract the day, month, and year and then use as you want.
In [9]: (y, m, d) = str(datetime.date.today()).split('-')[:3]
In [10]: y, m, d
Out[10]: ('2016', '11', '10')
The output is in string format, which can be converted to integers.
In [11]: int(y), int(m), int(d)
Out[11]: (2016, 11, 10)
Related
I am working with a date column in this form:
Date
1871.01
1871.02
...
1871.10
1871.11
So to convert the column to a datetimeindex, I use:
df["Date"].apply(lambda x: datetime.strptime(str(x), "%Y.%m"))
however the column is converted to:
Date
1871-01-01
1871-02-01
...
1871-01-01
1871-11-01
Does anyone have any idea of what causes this, where all "10"s convert to "01"s? Is there a better way to do this given my inputs are floats?
If the first format is a float, the 1871.10 and 1871.1 are exactly the same numbers. So the string of it will have the second value (the shortest one). But then it would seems it is January (month 1).
So you should stringify forcing two digits:
df["Date"].apply(lambda x: datetime.strptime("{:.2f}" % x, "%Y.%m"))
Note: the first format is very bad. The true solution is to correct it from beginning (e.g. when you read the input file you must tell the read function that the column is a date, not a float.
I have an int value that I've parsed through a series of string dates in a dataframe that looks like this:
last_date_daily = int(old_df_daily.iloc[2,-1])
print(last_date_daily)
Which gives:
20190106
I simply want to convert this integer to a datetime format.
I've tried:
last_date_daily = pd.to_datetime(last_date_daily)
Which leaves me with a swap of time to date and an incorrect date:
1970-01-01 00:00:00.020190106
Is there an easy way to make this conversion without having to split the year, month, and day by a - or / string?
You might want to check out dateparser.
import dateparser
dateparser.parse('20190106', date_formats=['%Y%m%d'])
>>> datetime.datetime(2019, 1, 6, 0 0)
to_datetime handles this case for strings:
In [11]: pd.to_datetime('20190106')
Out[11]: Timestamp('2019-01-06 00:00:00')
It's really not hard to break an integer into different fields.
>>> datetime.datetime(last_date_daily//10000, (last_date_daily//100)%100, last_date_daily%100)
datetime.datetime(2019, 1, 6, 0, 0)
You should be casting your integer to a string before calling to_datetime() on it. It should be:
last_date_daily = pd.to_datetime(str(last_date_daily))
i have a list of julian date:
42304.90625
I startet like this:
jDate=42304.90625
jDate=jDate[0:5]
print(datetime.datetime.strptime(jDate, '%y%j').date()
How can i extract the time?
Can somebody help me?
Thanks
From your use of %j in the conversion I take it you mean a mainframe "Julian" date of the form YYDDD. Start by splitting the value into an integer part and a fractional part:
>>> date, time = divmod(jDate, 1.0)
>>> date
42304.0
>>> time
0.90625
Using divmod() like this doesn't work on negative numbers, but a Julian date is never negative. Now convert the date to a string. A correct way to do that is:
date = str(int(date))
Doing this: jDate=jDate[0:5] doesn't work because jDate is a float. (The title of your posting says it's a string but your code says it's a float.)
Next convert the fractional day into seconds:
>>> datetime.timedelta(days=time)
datetime.timedelta(0, 78300)
and add the two together:
>>> datetime.datetime.strptime(date, '%y%j') + datetime.timedelta(days=0.90625)
datetime.datetime(2042, 10, 31, 21, 45)
I have only year parameter as input in the following manner:
2014,2015,2016
I want to convert each element from my list into python's datetime format. Is it possible to do this kind of things if the only given parameter is the year ?
Just set month and day manually to 1
from datetime import date
YearLst = [2014,2015,2016]
map(lambda t: date(t, 1, 1),YearLst)
i want to convert integer type date to datetime.
ex) i : 20130601000011( 2013-6-1 00:00: 11 )
i don't know exactly how to use pd.to_datetime
please any advice
thanks
ps. my script is below
rent_date_raw = pd.Series(1, rent['RENT_DATE'])
return_date_raw = pd.Series(1, rent['RETURN_DATE'])
rent_date = pd.Series([pd.to_datetime(date)
for date in rent_date_raw])
daily_rent_ts = rent_date.resample('D', how='count')
monthly_rent_ts = rent_date.resample('M', how='count')
Pandas seems to deal with your format fine as long as you convert to string first:
import pandas as pd
eg_date = 20130601000011
pd.to_datetime(str(eg_date))
Out[4]: Timestamp('2013-06-01 00:00:11')
Your data at the moment is really more of a string than an integer, since it doesn't really represent a single number. Different subparts of the string reflect different aspects of the time.