Removing Time from Date&Time Variable in Pandas

removing time from date&time variable in pandas?

Assuming all your datetime strings are in a similar format then just convert them to datetime using to_datetime and then call the dt.date attribute to get just the date portion:

In [37]:

df = pd.DataFrame({'date':['2015-02-21 12:08:51']})
df
Out[37]:
date
0 2015-02-21 12:08:51
In [39]:

df['date'] = pd.to_datetime(df['date']).dt.date
df
Out[39]:
date
0 2015-02-21

EDIT

If you just want to change the display and not the dtype then you can call dt.normalize:

In[10]:
df['date'] = pd.to_datetime(df['date']).dt.normalize()
df

Out[10]:
date
0 2015-02-21

You can see that the dtype remains as datetime:

In[11]:
df.dtypes

Out[11]:
date datetime64[ns]
dtype: object

Removing the timestamp from a datetime in pandas dataframe

You can do the following:

dfST['timestamp'] = pd.to_datetime(dfST['timestamp'])

to_datetime() will infer the formatting of the date column. You can also pass errors='coerce' if the column contains non-date values.

After completing the above, you'll be able to create a new column containing only date values:

dfST['new_date_column'] = dfST['timestamp'].dt.date

Remove time portion of DateTime index in pandas

You can maintain the datetime functionality and set the time portion to 00:00:00 with normalize.

df.index = df.index.normalize()

# For non-Index datetime64[ns] dtype columns you use the `.dt` accessor:
# df['column'] = df['column'].dt.normalize()


import pandas as pd
df = pd.DataFrame([1, 2, 3, 4], index=pd.date_range('2018', periods=4, freq='H'))

df.index = df.index.normalize()

print(df)
# 0
#2018-01-01 1
#2018-01-01 2
#2018-01-01 3
#2018-01-01 4

Looking at the index:

df.index
#DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None)

And the values are Timestamps:

df.index[0]
#Timestamp('2018-01-01 00:00:00')

How do I remove hours and seconds from my DataFrame column in python?

You can use pd.to_datetime to convert Date column to datetime object.

df['Date'] = pd.to_datetime(df['Date']).dt.date
# or
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
# or
df['Date'] = df['Date'].str.split(' ').str[0]

Python - Iterate over dataframe rows to remove time from datetime stamp

Use dt.floor:

df['Date'].dt.floor('D')

or

df['Date'].dt.normalize()

0 2016-06-01
1 2016-06-01
2 2016-07-01
3 2016-07-01
Name: Date, dtype: datetime64[ns]

Note: dt.floor returns datetime dtype
Where as dt.date returns string dtype

removing days from datetime type python

Assuming you want strings as output, you can clip, convert to string and slice:

df['result'] = df['col'].clip('0').astype(str).str[-8:]

output:

                    col    result
0 0 days 00:04:12 00:04:12
1 0 days 00:06:54 00:06:54
2 0 days 00:04:57 00:04:57
3 0 days 00:09:48 00:09:48
4 0 days 00:04:50 00:04:50
11839 0 days 00:06:39 00:06:39
11840 0 days 00:32:03 00:32:03
11841 0 days 00:02:32 00:02:32
11842 -1 days +00:16:52 00:00:00
11843 0 days 00:09:05 00:09:05

Note that any timedelta > 1 day will be incorrect, if you want you can clip to 1 day - 1 second:

df['result'] = df['col'].clip('0', '23h59m59s').astype(str).str[-8:]

example:

                col    result
0 1 days 00:09:05 23:59:59

Pandas, removing timestamp from datetime column using dt.date or dt.strftime converts column to dtype: object

IMO there is no issue here:

s = pd.to_datetime(pd.Series(['2021-02-01 00:00:00']))
s
# 0 2021-02-01
# dtype: datetime64[ns]

And indeed, the displayed type is "object":

s.dt.date
# 0 2021-02-01
# dtype: object

But this doesn't mean much, the type is really datetime.date:

type(s.dt.date[0])
# datetime.date


Related Topics



Leave a reply



Submit