Fix TypeError: datetime Is Not JSON Serializable
Fix Python datetime JSON serialization errors with ISO 8601, timezone-safe encoders, Flask, Django, FastAPI, pandas, debugging, tests, and prevention.
Python language features, pandas and scikit-learn recipes, and the errors that trip people up.
33 articles
Fix Python datetime JSON serialization errors with ISO 8601, timezone-safe encoders, Flask, Django, FastAPI, pandas, debugging, tests, and prevention.
Pandas 2.0 removed DataFrame.append(). Replace it with pd.concat — and if you were appending in a loop, collect rows in a list and build the DataFrame once.
Python 3.10 removed the old collections.Mapping alias. Import from collections.abc instead — but it's usually an old dependency, so read the traceback and upgrade it.
You import cv2 but the pip package is opencv-python — and there are four variants. Pick the right one, and preempt the libGL.so.1 error on servers with headless.
The package is python-dotenv but the import is dotenv — and pip install dotenv is the wrong package. Install python-dotenv into the interpreter you actually run.
This means the left side has fewer targets than the right side has values. Count both sides, iterate dicts with .items(), and use star-unpacking for extras.
Every TensorFlow CUDA library error — libcublas, libcudart, libnvinfer, cuDNN — traced to one of four root causes, with the order to diagnose them.
The pandas errors that waste the most time — SettingWithCopyWarning, Int64Index errors, silent assignment failures, and merge row explosions — and their causes.
Autocomplete fails for tf.keras because it's a lazily-loaded alias that static analyzers can't follow. Your code still runs — import from keras directly to give the IDE a real module to introspect.
This ImportError comes from a shadowed or half-installed package (classically TensorFlow) or a circular import — not from the abs builtin. Here's how to find which one and fix it.
Convert a date to Unix epoch time in Python with dt.timestamp(). Handle timezones correctly so a naive datetime isn't misread as local time.
Fix Python's str and NoneType concatenation error by finding the None value, repairing missing returns, choosing safe defaults, and avoiding common bad fixes.
Python inheritance for interviews: single and multiple inheritance, super() and cooperative __init__, the method resolution order (MRO / C3 linearisation) and the diamond problem, mixins, abstract base classes, and when to favour composition over inheritance.
A senior-level tour of Python OOP for interviews: classes and objects, __init__ and self, instance vs class vs static methods, @property, dunder methods, @dataclass, and the four pillars — encapsulation, abstraction, inheritance, and polymorphism.
How polymorphism really works in Python — duck typing (no shared base class needed), method overriding, operator overloading via dunder methods, functools.singledispatch, and Protocols — with the interview traps around Python's lack of method overloading.
OOPS, or Object-Oriented Programming Systems, is a programming paradigm that is based on the concept of objects, which have properties and methods.
Seaborn is a statistical plotting library on top of matplotlib. It works with pandas DataFrames and offers concise, attractive charts. Getting started.
This pandas error means you called a datetime accessor like .month on an integer index. Convert the index to datetimes first — and know that Int64Index itself was removed in pandas 2.0.
Append multiple Excel files in Python: read each with pandas, collect them in a list, then pd.concat once. Why concatenating in the loop is slow.
HTTP 524 is a Cloudflare timeout, not a Jupyter error — the origin took longer than 100 seconds to respond. Fix it by not blocking on long cells, or by bypassing the proxy with an SSH tunnel.
drop removes rows/columns by LABEL; boolean masking (df[df.a > 2]) keeps rows by CONDITION; df.filter() selects LABELS by name/regex — it does NOT take a boolean mask. Worked examples of each.
To implement a custom loss function in scikit-learn, we'll need to use the make_scorer function
Use XGBoost in Python via its scikit-learn API (XGBClassifier/Regressor) or native DMatrix API. Key hyperparameters and early stopping explained.
fit vs transform vs fit_transform in scikit-learn, and the rule that prevents data leakage: fit on train only, transform train and test with the training parameters. Plus why a Pipeline is the correct way to enforce this.
StratifiedKFold makes k non-overlapping folds (every sample tested once); StratifiedShuffleSplit draws independent random splits. When to use each.
Plot several DataFrame columns at once: pass a LIST to y (df.plot(y=['a','b'])), use x for a single shared axis, subplots=True for separate panels, and secondary_y for a dual axis.
Reduce repeated rows to a single row per key with groupby().agg(): sum/mean/first per column, collect repeated values into a list with agg(list), or reshape with pivot_table.
Get started with PyTorch: install it, create GPU-accelerated tensors, build a model with nn.Module, and train with autograd. A beginner's tutorial.
Python: ImportError: lxml not found, please install it
AttributeError: module 'pexpect' has no attribute 'TIMEOUT' means a stale module is loaded or a local file shadows the package. How to tell which, and fix it.
SettingWithCopyWarning means pandas can't tell if you're modifying a view or a copy, so your assignment may silently do nothing. The view-vs-copy .loc fix.
Fix Python Requests Max retries exceeded errors by identifying DNS, refused connection, timeout, TLS, proxy, or HTTP-status causes before adding retries.
Python's ternary is a conditional expression: value_if_true if condition else value_if_false — not C-style condition ? a : b. Syntax, examples, and limits.