You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
***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!**
606
+
***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!**
607
607
***`'fold=1'` means the second pass in case of time jumping back for one hour.**
608
608
***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.**
609
609
***Use `'<D/DT>.weekday()'` to get the day of the week as an int, with Monday being 0.**
@@ -1155,7 +1155,8 @@ class MySortable:
1155
1155
### Iterator
1156
1156
***Any object that has methods next() and iter() is an iterator.**
1157
1157
***Next() should return next item or raise StopIteration exception.**
1158
-
***Iter() should return 'self', i.e. unmodified object on which it was called.**
1158
+
***Iter() should return an iterator of remaining items, i.e. 'self'.**
1159
+
***Only objects that have iter() method can be used in for loops.**
1159
1160
```python
1160
1161
classCounter:
1161
1162
def__init__(self):
@@ -1181,7 +1182,7 @@ class Counter:
1181
1182
1182
1183
### Callable
1183
1184
***All functions and classes have a call() method, hence are callable.**
1184
-
***Use `'callable(<obj>)'` or `'isinstance(<obj>, collections.abc.Callable)'` to check if object is callable. Calling an uncallable object raises `'TypeError'`.**
1185
+
***Use `'callable(<obj>)'` or `'isinstance(<obj>, collections.abc.Callable)'` to check if object is callable. Calling an uncallable object raises TypeError.**
1185
1186
***When this cheatsheet uses `'<function>'` as an argument, it means `'<callable>'`.**
1186
1187
```python
1187
1188
classCounter:
@@ -1727,8 +1728,8 @@ os.remove(<path>) # Deletes the file.
1727
1728
os.rmdir(<path>) # Deletes the empty directory.
1728
1729
shutil.rmtree(<path>) # Deletes the directory.
1729
1730
```
1730
-
***Paths can be either strings, Paths, or DirEntry objects.**
1731
-
***Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).**
1731
+
***Paths can be either strings, Path objects, or DirEntry objects.**
1732
+
***Functions report OS related errors by raising OSError or one of its [subclasses](#exceptions-1).**
1732
1733
1733
1734
### Shell Commands
1734
1735
```python
@@ -2257,7 +2258,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
2257
2258
<Thread>.join() # Waits for the thread to finish executing.
2258
2259
```
2259
2260
***Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
2260
-
***Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
2261
+
***Use `'daemon=True'`, or program won't be able to exit while the thread is alive.**
2261
2262
2262
2263
### Lock
2263
2264
```python
@@ -3185,9 +3186,10 @@ Name: a, dtype: int64
3185
3186
<S>.plot.line/area/bar/pie/hist() # Generates a plot. `plt.show()` displays it.
3186
3187
```
3187
3188
***Use `'print(<S>.to_string())'` to print a Series that has more than 60 items.**
3188
-
***Indexing objects can't be tuples because `'obj[x, y]'` is converted to `'obj[(x, y)]'`.**
3189
+
***Use `'<S>.index'` to get collection of keys and `'<S>.index = <coll>'` to update them.**
3190
+
***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.**
3189
3191
***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.**
3190
-
***Series will silently overflow if we run `'pd.Series([100], dtype="int8") + 100'`!**
3192
+
***Series will silently overflow if you run `'pd.Series([100], dtype="int8") + 100'`!**
<li><strong>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!</strong></li>
539
+
<li><strong>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!</strong></li>
540
540
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
541
541
<li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M). Its str() method returns <codeclass="python hljs"><spanclass="hljs-string">'[±D, ]H:MM:SS[.…]'</span></code> and total_seconds() a float of all seconds.</strong></li>
542
542
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
@@ -988,7 +988,8 @@
988
988
<div><h3id="iterator-1">Iterator</h3><ul>
989
989
<li><strong>Any object that has methods next() and iter() is an iterator.</strong></li>
990
990
<li><strong>Next() should return next item or raise StopIteration exception.</strong></li>
991
-
<li><strong>Iter
10000
() should return 'self', i.e. unmodified object on which it was called.</strong></li>
991
+
<li><strong>Iter() should return an iterator of remaining items, i.e. 'self'.</strong></li>
992
+
<li><strong>Only objects that have iter() method can be used in for loops.</strong></li>
<li><strong>File objects returned by the <ahref="#open">open()</a> function, etc.</strong></li>
1012
1013
</ul><div><h3id="callable">Callable</h3><ul>
1013
1014
<li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
1014
-
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'callable(<obj>)'</span></code> or <codeclass="python hljs"><spanclass="hljs-string">'isinstance(<obj>, collections.abc.Callable)'</span></code> to check if object is callable. Calling an uncallable object raises <codeclass="python hljs"><spanclass="hljs-string">'TypeError'</span></code>.</strong></li>
1015
+
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'callable(<obj>)'</span></code> or <codeclass="python hljs"><spanclass="hljs-string">'isinstance(<obj>, collections.abc.Callable)'</span></code> to check if object is callable. Calling an uncallable object raises TypeError.</strong></li>
1015
1016
<li><strong>When this cheatsheet uses <codeclass="python hljs"><spanclass="hljs-string">'<function>'</span></code> as an argument, it means <codeclass="python hljs"><spanclass="hljs-string">'<callable>'</span></code>.</strong></li>
<str> = <pipe>.read(size=<spanclass="hljs-number">-1</span>) <spanclass="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
1868
-
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li>
1869
+
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'daemon=True'</span></code>, or program won't be able to exit while the thread is alive.</strong></li>
1869
1870
</ul>
1870
1871
<div><h3id="lock">Lock</h3><pre><codeclass="python language-python hljs"><lock> = Lock/RLock() <spanclass="hljs-comment"># RLock can only be released by acquirer.</span>
1871
1872
<lock>.acquire() <spanclass="hljs-comment"># Waits for the lock to be available.</span>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'print(<S>.to_string())'</span></code> to print a Series that has more than 60 items.</strong></li>
2615
-
<li><strong>Indexing objects can't be tuples because <codeclass="python hljs"><spanclass="hljs-string">'obj[x, y]'</span></code> is converted to <codeclass="python hljs"><spanclass="hljs-string">'obj[(x, y)]'</span></code>.</strong></li>
2616
+
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'<S>.index'</span></code> to get collection of keys and <codeclass="python hljs"><spanclass="hljs-string">'<S>.index = <coll>'</span></code> to update them.</strong></li>
2617
+
<li><strong>Only pass a list or Series to loc/iloc because <codeclass="python hljs"><spanclass="hljs-string">'obj[x, y]'</span></code> is converted to <codeclass="python hljs"><spanclass="hljs-string">'obj[(x, y)]'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'<S>.loc[key_1, key_2]'</span></code> is how you retrieve a value from a multi-indexed Series.</strong></li>
2616
2618
<li><strong>Pandas uses NumPy types like <codeclass="python hljs"><spanclass="hljs-string">'np.int64'</span></code>. Series is converted to <codeclass="python hljs"><spanclass="hljs-string">'float64'</span></code> if we assign np.nan to any item. Use <codeclass="python hljs"><spanclass="hljs-string">'<S>.astype(<str/type>)'</span></code> to get converted Series.</strong></li>
2617
-
<li><strong>Series will silently overflow if we run <codeclass="python hljs"><spanclass="hljs-string">'pd.Series([100], dtype="int8") + 100'</span></code>!</strong></li>
2619
+
<li><strong>Series will silently overflow if you run <codeclass="python hljs"><spanclass="hljs-string">'pd.Series([100], dtype="int8") + 100'</span></code>!</strong></li>
<li><strong>Last result has a multi-index. Use <codeclass="python hljs"><spanclass="hljs-string">'<S>[key_1, key_2]'</span></code> to get its values.</strong></li>
2642
-
</ul>
2643
2642
<div><h3id="dataframe">DataFrame</h3><p><strong>Table with labeled rows and columns.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>> </span>df = pd.DataFrame([[<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>], [<spanclass="hljs-number">3</span>, <spanclass="hljs-number">4</span>]], index=[<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>], columns=[<spanclass="hljs-string">'x'</span>, <spanclass="hljs-string">'y'</span>]); df
2644
2643
x y
2645
2644
a <spanclass="hljs-number">1</span><spanclass="hljs-number">2</span>
0 commit comments