Python f-strings are the best: – Put an f before the opening ‘ or “– Creates a regular string– Expressions in {} are evaluated/interpolated
Want to round Python Pandas datetimes? You have 3 options: – dt.floor — earlier– dt.ceil – later– dt.round — nearest For example:
Want to set the time on a Python Pandas series of datetimes to midnight? Use dt.normalize: You get back series of datetime values, but all times are 00:00:00.
Want to convert a Python Pandas datetime column to Unix time (seconds since 1970)? That’s it! No division needed.
Want to convert an integer Python Pandas column from Unix time to datetime? Use pd.to_datetime, passing the int column and the “unit” keyword argument, set to “s” (seconds): If the column is in ms, then say unit=’ms’
Want to check if two Python Pandas datetimes are in the same month/quarter/year? Convert them both to periods, and compare with ==:
Want to find which time period contains a Python Pandas datetime? Use to_period: This returns a Pandas “Period” object, useful (among other things) for checking if two dates are within the same period.
Want to find leap years in a Python Pandas datetime series? Use dt.is_leap_year:
Want to know if values in a Python Pandas datetime series are at the start/end of a period? Get a boolean series from:
Use dt.day_of_week to get the day number from a Python Pandas datetime series. But is 0 Sunday or Monday? Or not used at all? Better: Use the day_name method: Good news: It returns a series of strings!Bad news: It’s 4x slower.