From cd34692e33ff203a7e565063db8ad56e99cbe4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 30 Apr 2025 03:31:49 +0200 Subject: [PATCH] Datetime, Duck types, Pandas --- README.md | 19 ++++++++++--------- index.html | 25 ++++++++++++------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d833967..a193b50 100644 --- a/README.md +++ b/README.md @@ -603,7 +603,7 @@ import zoneinfo, dateutil.tz
= datetime(year, month, day, hour=0) # Also: `minute=0, second=0, microsecond=0, …`. = timedelta(weeks=0, days=0, hours=0) # Also: `minutes=0, seconds=0, microseconds=0`. ``` -* **Aware times and datetimes have defined timezone, while naive don't. If object is naive, it is presumed to be in the system's timezone!** +* **Times and datetimes that have defined timezone are called aware and ones that don't, naive. If object is naive, it is presumed to be in the system's timezone!** * **`'fold=1'` means the second pass in case of time jumping back for one hour.** * **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns `'[±D, ]H:MM:SS[.…]'` and total_seconds() a float of all seconds.** * **Use `'.weekday()'` to get the day of the week as an int, with Monday being 0.** @@ -1155,7 +1155,8 @@ class MySortable: ### Iterator * **Any object that has methods next() and iter() is an iterator.** * **Next() should return next item or raise StopIteration exception.** -* **Iter() should return 'self', i.e. unmodified object on which it was called.** +* **Iter() should return an iterator of remaining items, i.e. 'self'.** +* **Only objects that have iter() method can be used in for loops.** ```python class Counter: def __init__(self): @@ -1181,7 +1182,7 @@ class Counter: ### Callable * **All functions and classes have a call() method, hence are callable.** -* **Use `'callable()'` or `'isinstance(, collections.abc.Callable)'` to check if object is callable. Calling an uncallable object raises `'TypeError'`.** +* **Use `'callable()'` or `'isinstance(, collections.abc.Callable)'` to check if object is callable. Calling an uncallable object raises TypeError.** * **When this cheatsheet uses `''` as an argument, it means `''`.** ```python class Counter: @@ -1727,8 +1728,8 @@ os.remove() # Deletes the file. os.rmdir() # Deletes the empty directory. shutil.rmtree() # Deletes the directory. ``` -* **Paths can be either strings, Paths, or DirEntry objects.** -* **Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).** +* **Paths can be either strings, Path objects, or DirEntry objects.** +* **Functions report OS related errors by raising OSError or one of its [subclasses](#exceptions-1).** ### Shell Commands ```python @@ -2257,7 +2258,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed .join() # Waits for the thread to finish executing. ``` * **Use `'kwargs='` to pass keyword arguments to the function.** -* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.** +* **Use `'daemon=True'`, or program won't be able to exit while the thread is alive.** ### Lock ```python @@ -3185,9 +3186,10 @@ Name: a, dtype: int64 .plot.line/area/bar/pie/hist() # Generates a plot. `plt.show()` displays it. ``` * **Use `'print(.to_string())'` to print a Series that has more than 60 items.** -* **Indexing objects can't be tuples because `'obj[x, y]'` is converted to `'obj[(x, y)]'`.** +* **Use `'.index'` to get collection of keys and `'.index = '` to update them.** +* **Only pass a list or Series to loc/iloc because `'obj[x, y]'` is converted to `'obj[(x, y)]'` and `'.loc[key_1, key_2]'` is how you retrieve a value from a multi-indexed Series.** * **Pandas uses NumPy types like `'np.int64'`. Series is converted to `'float64'` if we assign np.nan to any item. Use `'.astype()'` to get converted Series.** -* **Series will silently overflow if we run `'pd.Series([100], dtype="int8") + 100'`!** +* **Series will silently overflow if you run `'pd.Series([100], dtype="int8") + 100'`!** #### Series — Aggregate, Transform, Map: ```python @@ -3214,7 +3216,6 @@ Name: a, dtype: int64 | | y 2.0 | y 2.0 | y 2.0 | +--------------+-------------+-------------+---------------+ ``` -* **Last result has a multi-index. Use `'[key_1, key_2]'` to get its values.** ### DataFrame **Table with labeled rows and columns.** diff --git a/index.html b/index.html index 891bd53..adae251 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@
- +
@@ -536,7 +536,7 @@ shuffle(<list>) # <TD> = timedelta(weeks=0, days=0, hours=0) # Also: `minutes=0, seconds=0, microseconds=0`.
    -
  • Aware times and datetimes have defined timezone, while naive don't. If object is naive, it is presumed to be in the system's timezone!
  • +
  • Times and datetimes that have defined timezone are called aware and ones that don't, naive. If object is naive, it is presumed to be in the system's timezone!
  • 'fold=1' means the second pass in case of time jumping back for one hour.
  • Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns '[±D, ]H:MM:SS[.…]' and total_seconds() a float of all seconds.
  • Use '<D/DT>.weekday()' to get the day of the week as an int, with Monday being 0.
  • @@ -988,7 +988,8 @@ P = make_dataclass('P', [(Iterator
    • Any object that has methods next() and iter() is an iterator.
    • Next() should return next item or raise StopIteration exception.
    • -
    • Iter() should return 'self', i.e. unmodified object on which it was called.
    • +
    • Iter() should return an iterator of remaining items, i.e. 'self'.
    • +
    • Only objects that have iter() method can be used in for loops.
    class Counter:
         def __init__(self):
             self.i = 0
    @@ -1011,7 +1012,7 @@ P = make_dataclass('P', [(open() function, etc.
     

Callable

  • All functions and classes have a call() method, hence are callable.
  • -
  • Use 'callable(<obj>)' or 'isinstance(<obj>, collections.abc.Callable)' to check if object is callable. Calling an uncallable object raises 'TypeError'.
  • +
  • Use 'callable(<obj>)' or 'isinstance(<obj>, collections.abc.Callable)' to check if object is callable. Calling an uncallable object raises TypeError.
  • When this cheatsheet uses '<function>' as an argument, it means '<callable>'.
class Counter:
     def __init__(self):
@@ -1460,8 +1461,8 @@ os.rmdir(<path>)                    # Deletes t
 shutil.rmtree(<path>)               # Deletes the directory.
 
    -
  • Paths can be either strings, Paths, or DirEntry objects.
  • -
  • Functions report OS related errors by raising either OSError or one of its subclasses.
  • +
  • Paths can be either strings, Path objects, or DirEntry objects.
  • +
  • Functions report OS related errors by raising OSError or one of its subclasses.

Shell Commands

<pipe> = os.popen('<commands>')     # Executes commands in sh/cmd. Returns combined stdout.
 <str>  = <pipe>.read(size=-1)       # Reads 'size' chars or until EOF. Also readline/s().
@@ -1865,7 +1866,7 @@ delattr(<obj>, '<name>')
 
 
  • Use 'kwargs=<dict>' to pass keyword arguments to the function.
  • -
  • Use 'daemon=True', or the program will not be able to exit while the thread is alive.
  • +
  • Use 'daemon=True', or program won't be able to exit while the thread is alive.

Lock

<lock> = Lock/RLock()                          # RLock can only be released by acquirer.
 <lock>.acquire()                               # Waits for the lock to be available.
@@ -2612,9 +2613,10 @@ Name: a, dtype: int64
 
  • Use 'print(<S>.to_string())' to print a Series that has more than 60 items.
  • -
  • Indexing objects can't be tuples because 'obj[x, y]' is converted to 'obj[(x, y)]'.
  • +
  • Use '<S>.index' to get collection of keys and '<S>.index = <coll>' to update them.
  • +
  • Only pass a list or Series to loc/iloc because 'obj[x, y]' is converted to 'obj[(x, y)]' and '<S>.loc[key_1, key_2]' is how you retrieve a value from a multi-indexed Series.
  • Pandas uses NumPy types like 'np.int64'. Series is converted to 'float64' if we assign np.nan to any item. Use '<S>.astype(<str/type>)' to get converted Series.
  • -
  • Series will silently overflow if we run 'pd.Series([100], dtype="int8") + 100'!
  • +
  • Series will silently overflow if you run 'pd.Series([100], dtype="int8") + 100'!

Series — Aggregate, Transform, Map:

<el> = <S>.sum/max/mean/std/idxmax/count()     # Or: <S>.agg(lambda <S>: <el>)
 <S>  = <S>.rank/diff/cumsum/ffill/interpol…()  # Or: <S>.agg/transform(lambda <S>: <S>)
@@ -2637,9 +2639,6 @@ Name: a, dtype: int64
 ┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
 
-
    -
  • Last result has a multi-index. Use '<S>[key_1, key_2]' to get its values.
  • -

DataFrame

Table with labeled rows and columns.

>>> df = pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']); df
    x  y
 a  1  2
@@ -2942,7 +2941,7 @@ $ deactivate                # Deactivates the active