home.social

Reuven M. Lerner

Helping you become fluent with Python and Pandas since 1995.

Posts
485
Followers
167
Following
51
Joined 2022-11-05 · View on fosstodon.org →
  1. methods are stored on a class, not an instance.

    Given x.m(), how does Python find it?

    The ICPO rule:

    - First, look for an attribute on the _i_nstance.
    - Not there? Look on the _c_lass.
    - Not here? Look on the _p_arent.
    - Not there? Look on _o_bject, the final parent.

  2. I just finished giving my talk at the education summit, "Vibe teaching: training in the age of AI."

    What fun!

    I've uploaded the slides: speakerdeck.com/reuven/vibe-te

  3. By default, every class inherits from "object".

    You can see this by checking the __bases__ attribute on a class:

    class C:
    pass

    print(C.__bases__) # prints (<class 'object'>,)

    __bases__ is a tuple — hinting (correctly) that Python supports multiple inheritance.

  4. It's time for ! We're in Long Beach, California, near one of the most active ports in the world.

    As such, Bamboo Weekly's challenges are about the Port of Long Beach — how much traffic it gets, what is imported, and from where.

    Level up your and : BambooWeekly.com

  5. How free is the press in your country? Challenge your skills — merging files, pivot tables, and plots — on the most recent data from Reporters Without Borders.

    Don't settle for toy data sets. Level up with real data and real challenges with Bamboo Weekly.

    Learn more at BambooWeekly.com

  6. The Strait of Hormuz remains closed. And US gas prices continue to rise.

    Bamboo Weekly challenges you to use to find where they're highest, how much they've increased — plus create interactive maps.

    Level up your data skills every Wednesday!

    Check it out: BambooWeekly.com

  7. Want to round datetimes? You have 3 options:

    - dt.floor — earlier
    - dt.ceil - later
    - dt.round — nearest

    For example:

    s.dt.floor('3h') # previous multiple-of-3 hour
    s.dt.ceil('15m') # next 15-minute block
    s.dt.round('1D') # nearest 1 day

  8. Want to set the time on a series of datetimes to midnight? Use dt.normalize:

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

    You get back series of datetime values, but all times are 00:00:00.

  9. Want to convert a datetime column to Unix time (seconds since 1970)?

    df['date'].astype('int64')

    That's it! No division needed.

  10. The Strait of Hormuz is still closed, and oil prices remain high.

    But which are higher, spot prices or oil futures?

    In the latest Bamboo Weekly, I give you six data-analysis problems to solve about oil prices using public data.

    More info: BambooWeekly.com

  11. Want to convert an integer column from Unix time to datetime?

    Use pd.to_datetime, passing the int column and the "unit" keyword argument, set to "s" (seconds):

    df['date'] = pd.to_datetime(df['unixtime'],unit='s')

    If the column is in ms, then say unit='ms'

  12. Want to check if two datetimes are in the same month/quarter/year?

    Convert them both to periods, and compare with ==:

    date1.to_period('M') == date2.to_period('M') # same month?
    date1.to_period('Q') == date2.to_period('Q') # same quarter?

  13. Want to find which time period contains a datetime? Use to_period:

    pd.Timestamp.now().to_period('1D')
    pd.Timestamp.now().to_period('6W')

    This returns a Pandas "Period" object, useful (among other things) for checking if two dates are within the same period.

  14. Want to find leap years in a datetime series? Use dt.is_leap_year:

    df = DataFrame({'x':np.arange(32),
    'y': pd.date_range(start='1995-04-19',
    end='2027-04-19', freq='1YE')})

    df.loc[pd.col('y').dt.is_leap_year] # returns boolean series

  15. Want to know if values in a datetime series are at the start/end of a period? Get a boolean series from:

    df['x'].dt.is_month_start # or dt.is_month_end
    df['x'].dt.is_quarter_start # or dt.is_quarter_end
    df['x'].dt.is_year_start # or dt.is_year_end

  16. Use dt.day_of_week to get the day number from a datetime series.

    But is 0 Sunday or Monday? Or not used at all?

    Better: Use the day_name method:

    df['x'].dt.day_name()

    Good news: It returns a series of strings!
    Bad news: It's 4x slower.

  17. Do Americans pay more taxes than in other countries? Do they pay more in income taxes?

    Explore OECD data with , , Marimo, and Claude Code (plus the new marimo-pair skill) in the latest Bamboo Weekly, and find out!

    Read all about it: BambooWeekly.com

  18. Want to grab a part of datetime value in a data frame column? Use "dt":

    df['x'].dt.year
    df['x'].dt.hour
    df['x'].dt.minute
    df['x'].dt.day_of_week # or dayofweek or weekday

    day_of_week returns an integer, where 0 is Monday and 6 is Sunday.

  19. It's tax day in the US — and according to Gallup, Americans think taxes are too high. But... how high are they vs. other countries?

    In the latest Bamboo Weekly, I pose 6 questions about taxes. Can you solve them with marimo-pair, Marimo, Claude Code, and ?

    Check it out: BambooWeekly.com

  20. Reading a CSV into , and parse_dates doesn't recognize the format? Pass the date_format keyword arg:

    df = pd.read_csv(filename,
    parse_dates=['x', 'y'],
    date_format='%Y-%m-%d %H:%M:%S')

    All strftime codes work!

  21. Reading a CSV into , and want a column to be treated as datetime values? Use parse_dates:

    df = pd.read_csv(filename,
    parse_dates=['x', 'y'])

    Bonus: The PyArrow engine often (not always) parses columns that look like dates.

  22. Want to take a series of strings, and get datetime values? Use pd.to_datetime:

    pd.to_datetime(df['x'])

    Notice: It's not a method! It's a top-level pd function.

    Specify a non-standard "format" with a strftime string:

    pd.to_datetime(df['x'], format='%d/%m/%Y')

  23. Happy 3rd anniversary, Bamboo Weekly!

    This is issue 156, marking 3 years of weekly Pandas challenges.

    This week, we look at Winter Olympic data. Since they love snow so much, we'll use along with .

    Check it out, at BambooWeekly.com.