Convert 'float' to 'date' [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I read a date in Excel format in Python?
I am reading values from a .XLS(excel) file.While reading a date field,i am getting a float value like 40374.What i need is to convert this float value into date.Please help.
Thanks in advance!!!!

Dates in excel are represented by the number of days since 1/1/1900. So 40374 is 40,374 days after 1/1/1900, or July 15, 2010
Also I believe that if there is anything after the decimal point, this represents a fraction of a day.
so 40374.0 would be 7/15/2010 at midnight, .5 would be noon, .75 is 6pm, etc.

Related

How to convert double values from df to year values/strings? [duplicate]

This question already has answers here:
Convert floats to ints in Pandas?
(11 answers)
Closed last year.
I have a dataframe with year values like: 2014.0, 2013.0... as float values. I would need to convert these values to year format, like 2014, 2013... I tried converting them to string, but I get "2014.0", "2013.0"...
How could I convert them to year, not double or strings?
This is what I tried for string conversion:
df['year']=df['year'].astype(str)
And then:
df['year']= pd.to_datetime(df['year'], format='%Y')
But this gave me results like: 1640995200000 and so on. How could I convert the doubles to reasonable year format?
simply do this !!works!!
df['year']=df['year'].astype(int)
output:
2004
2005
2017
converting to str does this 2014.0 => "2014.0" so that wont help you and converting using datetime will add whole date. so if you need only that particular year do this int remains datatype of that column not datetime objects

Change format of dates in Pandas DF [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Closed 12 months ago.
I have a pandas dataframe with dates in the following format:
Dec 11, 2018
Wondering is there an easy way to change the format to 11/12/2018? I know I can go through each month manually but not sure what my next step would be to switch around the month and day and add the /.
Thanks in advance!
Use strftime('%m/%d/%Y'):
s = pd.Series(['Dec 11, 2018'])
pd.to_datetime(s).dt.strftime('%m/%d/%Y')
Output:
0 12/11/2018
dtype: object

How to convert Python DataFrame column with Excel 5 digit date to yyyy-mm-dd [duplicate]

This question already has an answer here:
Pandas read_excel: parsing Excel datetime field correctly [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to convert an entire column containing a 5 digit date code (EX: 43390, 43599) to a normal date format. This is just to make data analysis easier, it doesn't matter which way it's formatted. In a series, the DATE column looks like this:
1 43390
2 43599
3 43605
4 43329
5 43330
...
264832 43533
264833 43325
264834 43410
264835 43461
264836 43365
I don't understand previous submissions with this question, and when I tried code such as
date_col = df.iloc[:,0]
print((datetime.utcfromtimestamp(0) + timedelta(date_col)).strftime("%Y-%m-%d"))
I get this error
unsupported type for timedelta days component: Series
Thanks, sorry if this is a basic question.
You are calling the dataframe and assigning it to date_col. If you want to get the value of your first row, for example, use date_col = df.iloc[0]. This will return the value.
Timedelta takes an integer value, not Series.

Python - convert string to simple YYYY date format [duplicate]

This question already has answers here:
Convert date to months and year
(4 answers)
Closed 4 years ago.
New to Python, and have already spent 30 minutes reviewing old responses, but still can't figure this out.
'Year' variable is a string. Examples: 1990, 2010.
I need to convert to date format, but just with the 4 year "digits".
Tried the following, but none are working:
date1 = datetime.datetime.date('Year', "%Y")
datetime.datetime.strftime('Year', "%Y")
wcData.astype(str).apply(lambda x: pd.to_datetime('Year', format='%Y'))
df.astype(str).apply(lambda x: pd.to_datetime(x, format='%Y%m%d'))
Please help!
You need datetime.datetime.strptime, not strftime:
import datetime
datetime.datetime.strptime("2010", "%Y")
Here's how to remember which one to use:
strPtime means "string -> parse -> time", that is, parse a string to create some kind of object that represents time;
strFtime means "string <- format <- time" (note the reversed arrows), that is, given an object that represents time, create a string representation of it using some format.

Subtract one date from another and get int [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 7 years ago.
How can I subtract on datefield from another and get result in integer?
Like 11.05.2015-10.05.2015 will return 1
I tried
entry.start_devation= each.start_on_fact - timedelta(days=data.calendar_start)
Take the difference between two dates, which is a timedelta, and ask for the days attribute of that timedelta.

Categories

Resources