keep only year month and day in datetime dataframe pandas [duplicate] - python

This question already has answers here:
Removing the timestamp from a datetime in pandas dataframe
(2 answers)
Closed 1 year ago.
Could anyone help me to delete the hours and minutes from this datetimes please?
I used this code but it stills returning the same output!
data["expected_Date"]=pd.to_datetime(data["Last_Date"]+ timedelta(days=365*2.9),format = '%Y-%m-%d')
but it returns always this type 2019-01-22 12:00:00 but I want to keep only this 2019-01-22
how can I manage with that please? Thank you!

data["expected_Date"].dt.date

Related

Converting datetime to date (Difference in Integer or Number format) [duplicate]

This question already has answers here:
Python: Convert timedelta to int in a dataframe
(6 answers)
Closed 1 year ago.
I have multiple columns with datetime format "14-09-2021 12:00:00 AM". I have converted them to date with the below code.
df['Column1'] = pd.to_datetime(df['Column1']).dt.date
df['Column2'] = pd.to_datetime(df['Column2']).dt.date
Intention is to take the difference between the two columns to identify the difference in days. When I take the difference df['diff']=df['Column1']-df['Column2'] I get the result as say "- 39 days" and not jus "- 39" (Image added below for clarity)
Intention is to get the difference in integer or number format (i.e just 39 and not 39 days), I'm not sure on how to go about this or when I have erred in the above code.
Help would be much appreciated.
Try this df['diff'] = df['diff'].astype(int)

Python pandas dataframe conversion to unixtime [duplicate]

This question already has answers here:
pandas datetime to unixtime
(2 answers)
Closed 2 years ago.
I would like to convert a date of the format with yyyy=year, mm=month, dd=day, hh=hour, nn=minute in a unix timestamp.
I tried:
df_out['unixtime'] = datetime(df_out['yyyymmddhhmm'].dt.year.to_numpy(),df_out['yyyymmddhhmm'].dt.month.to_numpy(),df_out['yyyymmddhhmm'].dt.day.to_numpy(),df_out['yyyymmddhhmm'].dt.hour.to_numpy(),df_out['yyyymmddhhmm'].dt.minute.to_numpy()).timestamp()
but I got the error message:
TypeError: only size-1 arrays can be converted to Python scalars
What am I doing wrong?
Any help is highly appreciated!
Regards,
Alexander
The officially recommended way is to subtract the epoch and then to floor-divide by the “unit” (1 second):
df = pd.DataFrame({'yyyymmddhhmm': pd.to_datetime(['20201108121314', '20201109121314'])})
df['unixtime'] = (df.yyyymmddhhmm - pd.Timestamp('1970-01-01')) // pd.Timedelta('1s')
Result:
yyyymmddhhmm unixtime
0 2020-11-08 12:13:14 1604837594
1 2020-11-09 12:13:14 1604923994
You can create a single column for the date using the pandas library
df_out['date_format'] = pd.to_datetime(df_out['date_time_column'], format='%Y%m%d%H%M')
Then you can create new columns which will consist of year, month, date, hour info by
pd.DatetimeIndex(df_out['date_format']).year
pd.DatetimeIndex(df_out['date_format']).month
pd.DatetimeIndex(df_out['date_format']).day
pd.DatetimeIndex(df_out['date_format']).hour

Remove "days 00:00:00"from dataframe [duplicate]

This question already has answers here:
Pandas Timedelta in Days
(5 answers)
Closed 3 years ago.
So, I have a pandas dataframe with a lot of variables including start/end date of loans.
I subtract these two in order to get their difference in days.
The result I get is of the type i.e. 349 days 00:00:00.
How can I keep only for example the number 349 from this column?
Check this format,
df['date'] = pd.to_timedelta(df['date'], errors='coerce').days
also, check .normalize() function in pandas.

how to convert object column in to datetime? [duplicate]

This question already has answers here:
ValueError: day is out of range for month
(2 answers)
Closed 3 years ago.
I am trying to convert a column type to datetime
Value Format in Column: '2016-04-10 12:17:52'
df['dropoff_time']
output
0 2016-04-10 12:17:52
1 2016-04-13 06:44:12
2 2016-04-13 06:54:43
3 2016-04-13 08:33:50
Name: created_at_new, Length: 328, dtype: object
I am trying the following code:
df['created_at_new'] = pd.to_datetime(df['created_at_new'])
ValueError: day is out of range for month
Desired result is a datetime
('2010-11-12 00:00:00')
When I tried with the same example, it worked for me. Anyways in order to rectify the error, you can try the following:
Check whether you have the latest version of pandas. If not Update it and
Try mentioning the date format
df['created_at_new'] = pd.to_datetime(df['created_at_new'], format='%Y-%m-%d %H:%M:%S')
Still, if it doesn't work. You can skip the one with error using the argument errors='coerce'. In the place of the skipped one, 'NaT' value will be added.
For more details, you check out this answer.

How to use Pandas calculate # of weeks/days? [duplicate]

This question already has answers here:
Pandas Datetime: Calculate Number of Weeks Between Dates in Two Columns
(2 answers)
Closed 4 years ago.
Hi, I have a dataframe with date columns. I want to add a column to calculate how many weeks since the contact? For example, today's date is 20-Sep-18, and use this date to calculate with the column.
Can anyone help me with this questions? Thanks!
You can do like this.
df['Contact Date']= pd.to_datetime(df['Contact Date'])
import datetime
df['How Many days'] = datetime.datetime.now() - df['Contact Date']

Categories