Use Python f-strings and format specifiers to make quick-and-dirty tables: Output: a…….10bcd……2e…..3456
A Python f-string can have a “format specifier,” one or more characters after : that dictate the format. For example, you can use < ^ and > with an integer to justify the text:
Want to print a Python expression and its value? The modern way: All expressions work:
Remember, Python f-strings are strings: 1. Use f-strings anywhere you’d use strings, e.g., filenames and URLs2. You don’t have to print an f-string; you can store it.3. Don’t overuse them! I see people writing when they can say
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 ==: