Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: bad operand type for unary ~: 'NoneType'

Tags:

python

I found a similar post and educational link (see below) but I'm not sure how to apply the answer logic from that post to my line of code.

My dataset looks like this

PT_FIN    PT_DISCH_DISPO
1         HOME/SELF CARE OP 
2         PARENT HOME/SELF CARE IP 
3         Admitted as inpatient
4         LEFT WITHOUT TREATMENT  
....

In total, there are over 100,000 records and below is a value count of the response options for the PT_DISCH_DISPO variable:

HOME/SELF CARE OP                    201253
PARENT HOME/SELF CARE IP              15313
Admitted as inpatient                 13721
LEFT WITHOUT TREATMENT                 4633
TRANS TO OTHER FACILITY OP              608
ERROR REGISTRATION                      227
LEFT AGAINST MED ADVICE OP              181
DSCHRG/TRNSFR PSYCH IP                  134
EXPIRED OTHER                            64
EXPIRED NO AUTOPSY NEEDED                61
DISCH/TRANS to INPT REHAB FAC            33
TRANS TO ACUTE CARE HOSP IP              30
NO SHOW                                  27
EXPIRED W/ AUTOPSY                       25
OTHER IP                                 24
RELATIVE HOME IP                         19
TRANS TO ADULT FACILITY OP               13
LEFT AGAINST MED ADVICE IP               12
TRANSFERRED TO SNF IP                    11
3RD PARTY PAYOR REQUEST FOR TRANS        10
EXPIRED/CORONER'S CASE                    6
FOSTER HOME IP                            5
HOME UNDER HOME HEALTH CARE IP            4
ORGANIZED GROUP HOME IP                   4
REHABILITATION CENTER IP                  3
DISCHARGED TO SNF OP                      2
Against Medical Advice                    2
CORRECTIONAL FACILITY IP                  2
TRANS TO INTERMED CARE FAC IP             1
Expired other                             1
DEAD ON ARRIVAL                           1

It's a pretty simple line of code:

#removing strings from dataset "~" denotes do not keep 
TAT_v2 = TAT_v1[~TAT_v1.PT_DISCH_DISPO.str.contains("error|no show|left without treatment", case=False)] 

It runs fine when I pill data from say 2019 onward but when I expand my dataframe to include data from 2018 and run this line of code I get the following error:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __invert__(self)
   1540     def __invert__(self):
   1541         try:
-> 1542             arr = operator.inv(com.values_from_object(self))
   1543             return self.__array_wrap__(arr)
   1544         except Exception:

TypeError: bad operand type for unary ~: 'NoneType'

Can someone please educate me as to why this error is occurring and how I can learn to fix it? Am I be receiving this error because I have Null values within my PT_DISCH_DISPO variable?

Many thanks!

python bad operand type for unary -: 'NoneType'

https://realpython.com/null-in-python/

like image 617
Raven Avatar asked Jan 24 '26 12:01

Raven


2 Answers

Ah all I have to do was add: na=False to my line of code

 TAT_v2 = TAT_v1[~TAT_v1.PT_DISCH_DISPO.str.contains("error|no show", na=False, case=False)] 
like image 200
Raven Avatar answered Jan 27 '26 00:01

Raven


As far as I understand your panda code, this line TAT_v1.PT_DISCH_DISPO.str.contains("error|no show|left without treatment", case=False) somehow returns None.
Investigate contains documentation may help

like image 24
Théo.B Avatar answered Jan 27 '26 01:01

Théo.B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!