글

라벨이 python인 게시물 표시

TypeError: 'NoneType' object is not subscriptable - 10 Causes and Fixes

이미지
TypeError: 'NoneType' object is not subscriptable means you used [...] on a value that is None . Python raises this the moment you write x[0] , x["key"] , or x[1:3] and x happens to be None - the subscript operator simply does not exist on the NoneType type. The traceback tells you where the subscript happened, not where the None came from. That gap is the whole debugging problem: the real bug is usually several lines (or several functions) earlier, at the point something returned None and you assumed it returned a dict, a list, or a string. This page lists the concrete ways a None gets into a subscript - functions with a missing return , mutating methods like sort() and append() , regex search() misses, dict.get() on an absent key, JSON null , DB queries with no rows - each with a reproduction, the fix, and a way to verify. If your case does not match any of them, work the diagnostic checklist instead. Error TypeError: 'NoneType' object i...

SettingWithCopyWarning: A value is trying to be set on a copy of a slice - 10 Causes and Fixes

이미지
You ran an assignment on a filtered DataFrame and pandas printed SettingWithCopyWarning: A value is trying to be set on a copy of a slice followed by from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead . The warning means pandas cannot tell whether the object you just wrote to is a view into the original DataFrame or an independent copy — so it cannot promise your write landed where you think it did. It is a warning, not an exception. Your script keeps running. That is exactly why it bites: half the time the write silently does nothing, and you find out three transformations later when a column is full of the values you thought you replaced. This page lists the ten patterns that actually produce it, each with a reproduction you can paste into a REPL and a fix that differs by the specific lines that matter. Then it explains the copy-versus-view design decision underneath, so you can spot the whole family of these on sight. Error SettingWithCopyWarning: ...