search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# Removing time unit from dates in Pandas

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

# When date is type datetime

Consider the following DataFrame:

``` dates = pd.date_range("2020-12-25 12:30:00", "2020-12-26 12:40:00")df = pd.DataFrame({"A":dates})df A0 2020-12-25 12:30:001 2020-12-26 12:30:00 ```

Here, column `A` is of type `datetime`. We can extract the date (without the time) like so:

``` df["B"] = df["A"].dt.normalize()df A B0 2020-12-25 2020-12-251 2020-12-26 2020-12-26 ```

Here, the `normalize()` method resets the time to midnight.

Note that the new column `B` is also of `datetime` as confirmed below:

``` df.dtypes A datetime64[ns]B datetime64[ns] ```

# When date is type DatetimeIndex

Consider the following `DatetimeIndex`:

``` dates = pd.date_range(start="2020-12-25 15:30:00", periods=3)dates DatetimeIndex(['2020-12-25 15:30:00', '2020-12-26 15:30:00', '2020-12-27 15:30:00'], dtype='datetime64[ns]', freq='D') ```

To remove the time unit from these dates, use the `normalize()` method:

``` dates.normalize() DatetimeIndex(['2020-12-25', '2020-12-26', '2020-12-27'], dtype='datetime64[ns]', freq='D') ```

`normalize()` resets the time unit to midnight.

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
2
settings
Enjoy our search
Hit / to insta-search docs and recipes!