series.py 182 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118
  1. """
  2. Data structure for 1-dimensional cross-sectional and time series data
  3. """
  4. from __future__ import annotations
  5. import sys
  6. from textwrap import dedent
  7. from typing import (
  8. IO,
  9. TYPE_CHECKING,
  10. Any,
  11. Callable,
  12. Hashable,
  13. Iterable,
  14. Literal,
  15. Mapping,
  16. Sequence,
  17. Union,
  18. cast,
  19. overload,
  20. )
  21. import warnings
  22. import weakref
  23. import numpy as np
  24. from pandas._config import (
  25. get_option,
  26. using_copy_on_write,
  27. )
  28. from pandas._libs import (
  29. lib,
  30. properties,
  31. reshape,
  32. )
  33. from pandas._libs.internals import BlockValuesRefs
  34. from pandas._libs.lib import is_range_indexer
  35. from pandas._typing import (
  36. AggFuncType,
  37. AlignJoin,
  38. AnyAll,
  39. AnyArrayLike,
  40. ArrayLike,
  41. Axis,
  42. AxisInt,
  43. CorrelationMethod,
  44. DropKeep,
  45. Dtype,
  46. DtypeBackend,
  47. DtypeObj,
  48. FilePath,
  49. FillnaOptions,
  50. Frequency,
  51. IgnoreRaise,
  52. IndexKeyFunc,
  53. IndexLabel,
  54. Level,
  55. NaPosition,
  56. QuantileInterpolation,
  57. Renamer,
  58. Scalar,
  59. SingleManager,
  60. SortKind,
  61. StorageOptions,
  62. TimedeltaConvertibleTypes,
  63. TimestampConvertibleTypes,
  64. ValueKeyFunc,
  65. WriteBuffer,
  66. npt,
  67. )
  68. from pandas.compat import PYPY
  69. from pandas.compat.numpy import function as nv
  70. from pandas.errors import (
  71. ChainedAssignmentError,
  72. InvalidIndexError,
  73. _chained_assignment_msg,
  74. )
  75. from pandas.util._decorators import (
  76. Appender,
  77. Substitution,
  78. doc,
  79. )
  80. from pandas.util._exceptions import find_stack_level
  81. from pandas.util._validators import (
  82. validate_ascending,
  83. validate_bool_kwarg,
  84. validate_percentile,
  85. )
  86. from pandas.core.dtypes.astype import astype_is_view
  87. from pandas.core.dtypes.cast import (
  88. LossySetitemError,
  89. convert_dtypes,
  90. maybe_box_native,
  91. maybe_cast_pointwise_result,
  92. )
  93. from pandas.core.dtypes.common import (
  94. ensure_platform_int,
  95. is_dict_like,
  96. is_extension_array_dtype,
  97. is_integer,
  98. is_iterator,
  99. is_list_like,
  100. is_numeric_dtype,
  101. is_object_dtype,
  102. is_scalar,
  103. pandas_dtype,
  104. validate_all_hashable,
  105. )
  106. from pandas.core.dtypes.generic import ABCDataFrame
  107. from pandas.core.dtypes.inference import is_hashable
  108. from pandas.core.dtypes.missing import (
  109. isna,
  110. na_value_for_dtype,
  111. notna,
  112. remove_na_arraylike,
  113. )
  114. from pandas.core import (
  115. algorithms,
  116. base,
  117. common as com,
  118. missing,
  119. nanops,
  120. ops,
  121. )
  122. from pandas.core.accessor import CachedAccessor
  123. from pandas.core.apply import SeriesApply
  124. from pandas.core.arrays import ExtensionArray
  125. from pandas.core.arrays.categorical import CategoricalAccessor
  126. from pandas.core.arrays.sparse import SparseAccessor
  127. from pandas.core.construction import (
  128. extract_array,
  129. sanitize_array,
  130. )
  131. from pandas.core.generic import NDFrame
  132. from pandas.core.indexers import (
  133. disallow_ndim_indexing,
  134. unpack_1tuple,
  135. )
  136. from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
  137. from pandas.core.indexes.api import (
  138. DatetimeIndex,
  139. Index,
  140. MultiIndex,
  141. PeriodIndex,
  142. default_index,
  143. ensure_index,
  144. )
  145. import pandas.core.indexes.base as ibase
  146. from pandas.core.indexes.multi import maybe_droplevels
  147. from pandas.core.indexing import (
  148. check_bool_indexer,
  149. check_dict_or_set_indexers,
  150. )
  151. from pandas.core.internals import (
  152. SingleArrayManager,
  153. SingleBlockManager,
  154. )
  155. from pandas.core.methods import selectn
  156. from pandas.core.shared_docs import _shared_docs
  157. from pandas.core.sorting import (
  158. ensure_key_mapped,
  159. nargsort,
  160. )
  161. from pandas.core.strings.accessor import StringMethods
  162. from pandas.core.tools.datetimes import to_datetime
  163. import pandas.io.formats.format as fmt
  164. from pandas.io.formats.info import (
  165. INFO_DOCSTRING,
  166. SeriesInfo,
  167. series_sub_kwargs,
  168. )
  169. import pandas.plotting
  170. if TYPE_CHECKING:
  171. from pandas._typing import (
  172. NumpySorter,
  173. NumpyValueArrayLike,
  174. Suffixes,
  175. )
  176. from pandas.core.frame import DataFrame
  177. from pandas.core.groupby.generic import SeriesGroupBy
  178. from pandas.core.resample import Resampler
  179. __all__ = ["Series"]
  180. _shared_doc_kwargs = {
  181. "axes": "index",
  182. "klass": "Series",
  183. "axes_single_arg": "{0 or 'index'}",
  184. "axis": """axis : {0 or 'index'}
  185. Unused. Parameter needed for compatibility with DataFrame.""",
  186. "inplace": """inplace : bool, default False
  187. If True, performs operation inplace and returns None.""",
  188. "unique": "np.ndarray",
  189. "duplicated": "Series",
  190. "optional_by": "",
  191. "optional_mapper": "",
  192. "optional_reindex": """
  193. index : array-like, optional
  194. New labels for the index. Preferably an Index object to avoid
  195. duplicating data.
  196. axis : int or str, optional
  197. Unused.""",
  198. "replace_iloc": """
  199. This differs from updating with ``.loc`` or ``.iloc``, which require
  200. you to specify a location to update with some value.""",
  201. }
  202. def _coerce_method(converter):
  203. """
  204. Install the scalar coercion methods.
  205. """
  206. def wrapper(self):
  207. if len(self) == 1:
  208. warnings.warn(
  209. f"Calling {converter.__name__} on a single element Series is "
  210. "deprecated and will raise a TypeError in the future. "
  211. f"Use {converter.__name__}(ser.iloc[0]) instead",
  212. FutureWarning,
  213. stacklevel=find_stack_level(),
  214. )
  215. return converter(self.iloc[0])
  216. raise TypeError(f"cannot convert the series to {converter}")
  217. wrapper.__name__ = f"__{converter.__name__}__"
  218. return wrapper
  219. # ----------------------------------------------------------------------
  220. # Series class
  221. # error: Definition of "max" in base class "IndexOpsMixin" is incompatible with
  222. # definition in base class "NDFrame"
  223. # error: Definition of "min" in base class "IndexOpsMixin" is incompatible with
  224. # definition in base class "NDFrame"
  225. class Series(base.IndexOpsMixin, NDFrame): # type: ignore[misc]
  226. """
  227. One-dimensional ndarray with axis labels (including time series).
  228. Labels need not be unique but must be a hashable type. The object
  229. supports both integer- and label-based indexing and provides a host of
  230. methods for performing operations involving the index. Statistical
  231. methods from ndarray have been overridden to automatically exclude
  232. missing data (currently represented as NaN).
  233. Operations between Series (+, -, /, \\*, \\*\\*) align values based on their
  234. associated index values-- they need not be the same length. The result
  235. index will be the sorted union of the two indexes.
  236. Parameters
  237. ----------
  238. data : array-like, Iterable, dict, or scalar value
  239. Contains data stored in Series. If data is a dict, argument order is
  240. maintained.
  241. index : array-like or Index (1d)
  242. Values must be hashable and have the same length as `data`.
  243. Non-unique index values are allowed. Will default to
  244. RangeIndex (0, 1, 2, ..., n) if not provided. If data is dict-like
  245. and index is None, then the keys in the data are used as the index. If the
  246. index is not None, the resulting Series is reindexed with the index values.
  247. dtype : str, numpy.dtype, or ExtensionDtype, optional
  248. Data type for the output Series. If not specified, this will be
  249. inferred from `data`.
  250. See the :ref:`user guide <basics.dtypes>` for more usages.
  251. name : Hashable, default None
  252. The name to give to the Series.
  253. copy : bool, default False
  254. Copy input data. Only affects Series or 1d ndarray input. See examples.
  255. Notes
  256. -----
  257. Please reference the :ref:`User Guide <basics.series>` for more information.
  258. Examples
  259. --------
  260. Constructing Series from a dictionary with an Index specified
  261. >>> d = {'a': 1, 'b': 2, 'c': 3}
  262. >>> ser = pd.Series(data=d, index=['a', 'b', 'c'])
  263. >>> ser
  264. a 1
  265. b 2
  266. c 3
  267. dtype: int64
  268. The keys of the dictionary match with the Index values, hence the Index
  269. values have no effect.
  270. >>> d = {'a': 1, 'b': 2, 'c': 3}
  271. >>> ser = pd.Series(data=d, index=['x', 'y', 'z'])
  272. >>> ser
  273. x NaN
  274. y NaN
  275. z NaN
  276. dtype: float64
  277. Note that the Index is first build with the keys from the dictionary.
  278. After this the Series is reindexed with the given Index values, hence we
  279. get all NaN as a result.
  280. Constructing Series from a list with `copy=False`.
  281. >>> r = [1, 2]
  282. >>> ser = pd.Series(r, copy=False)
  283. >>> ser.iloc[0] = 999
  284. >>> r
  285. [1, 2]
  286. >>> ser
  287. 0 999
  288. 1 2
  289. dtype: int64
  290. Due to input data type the Series has a `copy` of
  291. the original data even though `copy=False`, so
  292. the data is unchanged.
  293. Constructing Series from a 1d ndarray with `copy=False`.
  294. >>> r = np.array([1, 2])
  295. >>> ser = pd.Series(r, copy=False)
  296. >>> ser.iloc[0] = 999
  297. >>> r
  298. array([999, 2])
  299. >>> ser
  300. 0 999
  301. 1 2
  302. dtype: int64
  303. Due to input data type the Series has a `view` on
  304. the original data, so
  305. the data is changed as well.
  306. """
  307. _typ = "series"
  308. _HANDLED_TYPES = (Index, ExtensionArray, np.ndarray)
  309. _name: Hashable
  310. _metadata: list[str] = ["name"]
  311. _internal_names_set = {"index"} | NDFrame._internal_names_set
  312. _accessors = {"dt", "cat", "str", "sparse"}
  313. _hidden_attrs = (
  314. base.IndexOpsMixin._hidden_attrs | NDFrame._hidden_attrs | frozenset([])
  315. )
  316. # Override cache_readonly bc Series is mutable
  317. # error: Incompatible types in assignment (expression has type "property",
  318. # base class "IndexOpsMixin" defined the type as "Callable[[IndexOpsMixin], bool]")
  319. hasnans = property( # type: ignore[assignment]
  320. # error: "Callable[[IndexOpsMixin], bool]" has no attribute "fget"
  321. base.IndexOpsMixin.hasnans.fget, # type: ignore[attr-defined]
  322. doc=base.IndexOpsMixin.hasnans.__doc__,
  323. )
  324. _mgr: SingleManager
  325. div: Callable[[Series, Any], Series]
  326. rdiv: Callable[[Series, Any], Series]
  327. # ----------------------------------------------------------------------
  328. # Constructors
  329. def __init__(
  330. self,
  331. data=None,
  332. index=None,
  333. dtype: Dtype | None = None,
  334. name=None,
  335. copy: bool | None = None,
  336. fastpath: bool = False,
  337. ) -> None:
  338. if (
  339. isinstance(data, (SingleBlockManager, SingleArrayManager))
  340. and index is None
  341. and dtype is None
  342. and (copy is False or copy is None)
  343. ):
  344. if using_copy_on_write():
  345. data = data.copy(deep=False)
  346. # GH#33357 called with just the SingleBlockManager
  347. NDFrame.__init__(self, data)
  348. if fastpath:
  349. # e.g. from _box_col_values, skip validation of name
  350. object.__setattr__(self, "_name", name)
  351. else:
  352. self.name = name
  353. return
  354. if isinstance(data, (ExtensionArray, np.ndarray)):
  355. if copy is not False and using_copy_on_write():
  356. if dtype is None or astype_is_view(data.dtype, pandas_dtype(dtype)):
  357. data = data.copy()
  358. if copy is None:
  359. copy = False
  360. # we are called internally, so short-circuit
  361. if fastpath:
  362. # data is a ndarray, index is defined
  363. if not isinstance(data, (SingleBlockManager, SingleArrayManager)):
  364. manager = get_option("mode.data_manager")
  365. if manager == "block":
  366. data = SingleBlockManager.from_array(data, index)
  367. elif manager == "array":
  368. data = SingleArrayManager.from_array(data, index)
  369. elif using_copy_on_write() and not copy:
  370. data = data.copy(deep=False)
  371. if copy:
  372. data = data.copy()
  373. # skips validation of the name
  374. object.__setattr__(self, "_name", name)
  375. NDFrame.__init__(self, data)
  376. return
  377. if isinstance(data, SingleBlockManager) and using_copy_on_write() and not copy:
  378. data = data.copy(deep=False)
  379. name = ibase.maybe_extract_name(name, data, type(self))
  380. if index is not None:
  381. index = ensure_index(index)
  382. if dtype is not None:
  383. dtype = self._validate_dtype(dtype)
  384. if data is None:
  385. index = index if index is not None else default_index(0)
  386. if len(index) or dtype is not None:
  387. data = na_value_for_dtype(pandas_dtype(dtype), compat=False)
  388. else:
  389. data = []
  390. if isinstance(data, MultiIndex):
  391. raise NotImplementedError(
  392. "initializing a Series from a MultiIndex is not supported"
  393. )
  394. refs = None
  395. if isinstance(data, Index):
  396. if dtype is not None:
  397. data = data.astype(dtype, copy=False)
  398. if using_copy_on_write():
  399. refs = data._references
  400. data = data._values
  401. else:
  402. # GH#24096 we need to ensure the index remains immutable
  403. data = data._values.copy()
  404. copy = False
  405. elif isinstance(data, np.ndarray):
  406. if len(data.dtype):
  407. # GH#13296 we are dealing with a compound dtype, which
  408. # should be treated as 2D
  409. raise ValueError(
  410. "Cannot construct a Series from an ndarray with "
  411. "compound dtype. Use DataFrame instead."
  412. )
  413. elif isinstance(data, Series):
  414. if index is None:
  415. index = data.index
  416. data = data._mgr.copy(deep=False)
  417. else:
  418. data = data.reindex(index, copy=copy)
  419. copy = False
  420. data = data._mgr
  421. elif is_dict_like(data):
  422. data, index = self._init_dict(data, index, dtype)
  423. dtype = None
  424. copy = False
  425. elif isinstance(data, (SingleBlockManager, SingleArrayManager)):
  426. if index is None:
  427. index = data.index
  428. elif not data.index.equals(index) or copy:
  429. # GH#19275 SingleBlockManager input should only be called
  430. # internally
  431. raise AssertionError(
  432. "Cannot pass both SingleBlockManager "
  433. "`data` argument and a different "
  434. "`index` argument. `copy` must be False."
  435. )
  436. elif isinstance(data, ExtensionArray):
  437. pass
  438. else:
  439. data = com.maybe_iterable_to_list(data)
  440. if is_list_like(data) and not len(data) and dtype is None:
  441. # GH 29405: Pre-2.0, this defaulted to float.
  442. dtype = np.dtype(object)
  443. if index is None:
  444. if not is_list_like(data):
  445. data = [data]
  446. index = default_index(len(data))
  447. elif is_list_like(data):
  448. com.require_length_match(data, index)
  449. # create/copy the manager
  450. if isinstance(data, (SingleBlockManager, SingleArrayManager)):
  451. if dtype is not None:
  452. data = data.astype(dtype=dtype, errors="ignore", copy=copy)
  453. elif copy:
  454. data = data.copy()
  455. else:
  456. data = sanitize_array(data, index, dtype, copy)
  457. manager = get_option("mode.data_manager")
  458. if manager == "block":
  459. data = SingleBlockManager.from_array(data, index, refs=refs)
  460. elif manager == "array":
  461. data = SingleArrayManager.from_array(data, index)
  462. NDFrame.__init__(self, data)
  463. self.name = name
  464. self._set_axis(0, index)
  465. def _init_dict(
  466. self, data, index: Index | None = None, dtype: DtypeObj | None = None
  467. ):
  468. """
  469. Derive the "_mgr" and "index" attributes of a new Series from a
  470. dictionary input.
  471. Parameters
  472. ----------
  473. data : dict or dict-like
  474. Data used to populate the new Series.
  475. index : Index or None, default None
  476. Index for the new Series: if None, use dict keys.
  477. dtype : np.dtype, ExtensionDtype, or None, default None
  478. The dtype for the new Series: if None, infer from data.
  479. Returns
  480. -------
  481. _data : BlockManager for the new Series
  482. index : index for the new Series
  483. """
  484. keys: Index | tuple
  485. # Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')]
  486. # raises KeyError), so we iterate the entire dict, and align
  487. if data:
  488. # GH:34717, issue was using zip to extract key and values from data.
  489. # using generators in effects the performance.
  490. # Below is the new way of extracting the keys and values
  491. keys = tuple(data.keys())
  492. values = list(data.values()) # Generating list of values- faster way
  493. elif index is not None:
  494. # fastpath for Series(data=None). Just use broadcasting a scalar
  495. # instead of reindexing.
  496. if len(index) or dtype is not None:
  497. values = na_value_for_dtype(pandas_dtype(dtype), compat=False)
  498. else:
  499. values = []
  500. keys = index
  501. else:
  502. keys, values = default_index(0), []
  503. # Input is now list-like, so rely on "standard" construction:
  504. s = Series(values, index=keys, dtype=dtype)
  505. # Now we just make sure the order is respected, if any
  506. if data and index is not None:
  507. s = s.reindex(index, copy=False)
  508. return s._mgr, s.index
  509. # ----------------------------------------------------------------------
  510. @property
  511. def _constructor(self) -> Callable[..., Series]:
  512. return Series
  513. @property
  514. def _constructor_expanddim(self) -> Callable[..., DataFrame]:
  515. """
  516. Used when a manipulation result has one higher dimension as the
  517. original, such as Series.to_frame()
  518. """
  519. from pandas.core.frame import DataFrame
  520. return DataFrame
  521. # types
  522. @property
  523. def _can_hold_na(self) -> bool:
  524. return self._mgr._can_hold_na
  525. # ndarray compatibility
  526. @property
  527. def dtype(self) -> DtypeObj:
  528. """
  529. Return the dtype object of the underlying data.
  530. Examples
  531. --------
  532. >>> s = pd.Series([1, 2, 3])
  533. >>> s.dtype
  534. dtype('int64')
  535. """
  536. return self._mgr.dtype
  537. @property
  538. def dtypes(self) -> DtypeObj:
  539. """
  540. Return the dtype object of the underlying data.
  541. Examples
  542. --------
  543. >>> s = pd.Series([1, 2, 3])
  544. >>> s.dtypes
  545. dtype('int64')
  546. """
  547. # DataFrame compatibility
  548. return self.dtype
  549. @property
  550. def name(self) -> Hashable:
  551. """
  552. Return the name of the Series.
  553. The name of a Series becomes its index or column name if it is used
  554. to form a DataFrame. It is also used whenever displaying the Series
  555. using the interpreter.
  556. Returns
  557. -------
  558. label (hashable object)
  559. The name of the Series, also the column name if part of a DataFrame.
  560. See Also
  561. --------
  562. Series.rename : Sets the Series name when given a scalar input.
  563. Index.name : Corresponding Index property.
  564. Examples
  565. --------
  566. The Series name can be set initially when calling the constructor.
  567. >>> s = pd.Series([1, 2, 3], dtype=np.int64, name='Numbers')
  568. >>> s
  569. 0 1
  570. 1 2
  571. 2 3
  572. Name: Numbers, dtype: int64
  573. >>> s.name = "Integers"
  574. >>> s
  575. 0 1
  576. 1 2
  577. 2 3
  578. Name: Integers, dtype: int64
  579. The name of a Series within a DataFrame is its column name.
  580. >>> df = pd.DataFrame([[1, 2], [3, 4], [5, 6]],
  581. ... columns=["Odd Numbers", "Even Numbers"])
  582. >>> df
  583. Odd Numbers Even Numbers
  584. 0 1 2
  585. 1 3 4
  586. 2 5 6
  587. >>> df["Even Numbers"].name
  588. 'Even Numbers'
  589. """
  590. return self._name
  591. @name.setter
  592. def name(self, value: Hashable) -> None:
  593. validate_all_hashable(value, error_name=f"{type(self).__name__}.name")
  594. object.__setattr__(self, "_name", value)
  595. @property
  596. def values(self):
  597. """
  598. Return Series as ndarray or ndarray-like depending on the dtype.
  599. .. warning::
  600. We recommend using :attr:`Series.array` or
  601. :meth:`Series.to_numpy`, depending on whether you need
  602. a reference to the underlying data or a NumPy array.
  603. Returns
  604. -------
  605. numpy.ndarray or ndarray-like
  606. See Also
  607. --------
  608. Series.array : Reference to the underlying data.
  609. Series.to_numpy : A NumPy array representing the underlying data.
  610. Examples
  611. --------
  612. >>> pd.Series([1, 2, 3]).values
  613. array([1, 2, 3])
  614. >>> pd.Series(list('aabc')).values
  615. array(['a', 'a', 'b', 'c'], dtype=object)
  616. >>> pd.Series(list('aabc')).astype('category').values
  617. ['a', 'a', 'b', 'c']
  618. Categories (3, object): ['a', 'b', 'c']
  619. Timezone aware datetime data is converted to UTC:
  620. >>> pd.Series(pd.date_range('20130101', periods=3,
  621. ... tz='US/Eastern')).values
  622. array(['2013-01-01T05:00:00.000000000',
  623. '2013-01-02T05:00:00.000000000',
  624. '2013-01-03T05:00:00.000000000'], dtype='datetime64[ns]')
  625. """
  626. return self._mgr.external_values()
  627. @property
  628. def _values(self):
  629. """
  630. Return the internal repr of this data (defined by Block.interval_values).
  631. This are the values as stored in the Block (ndarray or ExtensionArray
  632. depending on the Block class), with datetime64[ns] and timedelta64[ns]
  633. wrapped in ExtensionArrays to match Index._values behavior.
  634. Differs from the public ``.values`` for certain data types, because of
  635. historical backwards compatibility of the public attribute (e.g. period
  636. returns object ndarray and datetimetz a datetime64[ns] ndarray for
  637. ``.values`` while it returns an ExtensionArray for ``._values`` in those
  638. cases).
  639. Differs from ``.array`` in that this still returns the numpy array if
  640. the Block is backed by a numpy array (except for datetime64 and
  641. timedelta64 dtypes), while ``.array`` ensures to always return an
  642. ExtensionArray.
  643. Overview:
  644. dtype | values | _values | array |
  645. ----------- | ------------- | ------------- | ------------- |
  646. Numeric | ndarray | ndarray | PandasArray |
  647. Category | Categorical | Categorical | Categorical |
  648. dt64[ns] | ndarray[M8ns] | DatetimeArray | DatetimeArray |
  649. dt64[ns tz] | ndarray[M8ns] | DatetimeArray | DatetimeArray |
  650. td64[ns] | ndarray[m8ns] | TimedeltaArray| ndarray[m8ns] |
  651. Period | ndarray[obj] | PeriodArray | PeriodArray |
  652. Nullable | EA | EA | EA |
  653. """
  654. return self._mgr.internal_values()
  655. @property
  656. def _references(self) -> BlockValuesRefs | None:
  657. if isinstance(self._mgr, SingleArrayManager):
  658. return None
  659. return self._mgr._block.refs
  660. # error: Decorated property not supported
  661. @Appender(base.IndexOpsMixin.array.__doc__) # type: ignore[misc]
  662. @property
  663. def array(self) -> ExtensionArray:
  664. return self._mgr.array_values()
  665. # ops
  666. def ravel(self, order: str = "C") -> ArrayLike:
  667. """
  668. Return the flattened underlying data as an ndarray or ExtensionArray.
  669. Returns
  670. -------
  671. numpy.ndarray or ExtensionArray
  672. Flattened data of the Series.
  673. See Also
  674. --------
  675. numpy.ndarray.ravel : Return a flattened array.
  676. """
  677. arr = self._values.ravel(order=order)
  678. if isinstance(arr, np.ndarray) and using_copy_on_write():
  679. arr.flags.writeable = False
  680. return arr
  681. def __len__(self) -> int:
  682. """
  683. Return the length of the Series.
  684. """
  685. return len(self._mgr)
  686. def view(self, dtype: Dtype | None = None) -> Series:
  687. """
  688. Create a new view of the Series.
  689. This function will return a new Series with a view of the same
  690. underlying values in memory, optionally reinterpreted with a new data
  691. type. The new data type must preserve the same size in bytes as to not
  692. cause index misalignment.
  693. Parameters
  694. ----------
  695. dtype : data type
  696. Data type object or one of their string representations.
  697. Returns
  698. -------
  699. Series
  700. A new Series object as a view of the same data in memory.
  701. See Also
  702. --------
  703. numpy.ndarray.view : Equivalent numpy function to create a new view of
  704. the same data in memory.
  705. Notes
  706. -----
  707. Series are instantiated with ``dtype=float64`` by default. While
  708. ``numpy.ndarray.view()`` will return a view with the same data type as
  709. the original array, ``Series.view()`` (without specified dtype)
  710. will try using ``float64`` and may fail if the original data type size
  711. in bytes is not the same.
  712. Examples
  713. --------
  714. >>> s = pd.Series([-2, -1, 0, 1, 2], dtype='int8')
  715. >>> s
  716. 0 -2
  717. 1 -1
  718. 2 0
  719. 3 1
  720. 4 2
  721. dtype: int8
  722. The 8 bit signed integer representation of `-1` is `0b11111111`, but
  723. the same bytes represent 255 if read as an 8 bit unsigned integer:
  724. >>> us = s.view('uint8')
  725. >>> us
  726. 0 254
  727. 1 255
  728. 2 0
  729. 3 1
  730. 4 2
  731. dtype: uint8
  732. The views share the same underlying values:
  733. >>> us[0] = 128
  734. >>> s
  735. 0 -128
  736. 1 -1
  737. 2 0
  738. 3 1
  739. 4 2
  740. dtype: int8
  741. """
  742. # self.array instead of self._values so we piggyback on PandasArray
  743. # implementation
  744. res_values = self.array.view(dtype)
  745. res_ser = self._constructor(res_values, index=self.index, copy=False)
  746. if isinstance(res_ser._mgr, SingleBlockManager) and using_copy_on_write():
  747. blk = res_ser._mgr._block
  748. blk.refs = cast("BlockValuesRefs", self._references)
  749. blk.refs.add_reference(blk) # type: ignore[arg-type]
  750. return res_ser.__finalize__(self, method="view")
  751. # ----------------------------------------------------------------------
  752. # NDArray Compat
  753. _HANDLED_TYPES = (Index, ExtensionArray, np.ndarray)
  754. def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
  755. """
  756. Return the values as a NumPy array.
  757. Users should not call this directly. Rather, it is invoked by
  758. :func:`numpy.array` and :func:`numpy.asarray`.
  759. Parameters
  760. ----------
  761. dtype : str or numpy.dtype, optional
  762. The dtype to use for the resulting NumPy array. By default,
  763. the dtype is inferred from the data.
  764. Returns
  765. -------
  766. numpy.ndarray
  767. The values in the series converted to a :class:`numpy.ndarray`
  768. with the specified `dtype`.
  769. See Also
  770. --------
  771. array : Create a new array from data.
  772. Series.array : Zero-copy view to the array backing the Series.
  773. Series.to_numpy : Series method for similar behavior.
  774. Examples
  775. --------
  776. >>> ser = pd.Series([1, 2, 3])
  777. >>> np.asarray(ser)
  778. array([1, 2, 3])
  779. For timezone-aware data, the timezones may be retained with
  780. ``dtype='object'``
  781. >>> tzser = pd.Series(pd.date_range('2000', periods=2, tz="CET"))
  782. >>> np.asarray(tzser, dtype="object")
  783. array([Timestamp('2000-01-01 00:00:00+0100', tz='CET'),
  784. Timestamp('2000-01-02 00:00:00+0100', tz='CET')],
  785. dtype=object)
  786. Or the values may be localized to UTC and the tzinfo discarded with
  787. ``dtype='datetime64[ns]'``
  788. >>> np.asarray(tzser, dtype="datetime64[ns]") # doctest: +ELLIPSIS
  789. array(['1999-12-31T23:00:00.000000000', ...],
  790. dtype='datetime64[ns]')
  791. """
  792. values = self._values
  793. arr = np.asarray(values, dtype=dtype)
  794. if using_copy_on_write() and astype_is_view(values.dtype, arr.dtype):
  795. arr = arr.view()
  796. arr.flags.writeable = False
  797. return arr
  798. # ----------------------------------------------------------------------
  799. # Unary Methods
  800. # coercion
  801. __float__ = _coerce_method(float)
  802. __int__ = _coerce_method(int)
  803. # ----------------------------------------------------------------------
  804. # indexers
  805. @property
  806. def axes(self) -> list[Index]:
  807. """
  808. Return a list of the row axis labels.
  809. """
  810. return [self.index]
  811. # ----------------------------------------------------------------------
  812. # Indexing Methods
  813. @Appender(NDFrame.take.__doc__)
  814. def take(self, indices, axis: Axis = 0, **kwargs) -> Series:
  815. nv.validate_take((), kwargs)
  816. indices = ensure_platform_int(indices)
  817. if (
  818. indices.ndim == 1
  819. and using_copy_on_write()
  820. and is_range_indexer(indices, len(self))
  821. ):
  822. return self.copy(deep=None)
  823. new_index = self.index.take(indices)
  824. new_values = self._values.take(indices)
  825. result = self._constructor(new_values, index=new_index, fastpath=True)
  826. return result.__finalize__(self, method="take")
  827. def _take_with_is_copy(self, indices, axis: Axis = 0) -> Series:
  828. """
  829. Internal version of the `take` method that sets the `_is_copy`
  830. attribute to keep track of the parent dataframe (using in indexing
  831. for the SettingWithCopyWarning). For Series this does the same
  832. as the public take (it never sets `_is_copy`).
  833. See the docstring of `take` for full explanation of the parameters.
  834. """
  835. return self.take(indices=indices, axis=axis)
  836. def _ixs(self, i: int, axis: AxisInt = 0) -> Any:
  837. """
  838. Return the i-th value or values in the Series by location.
  839. Parameters
  840. ----------
  841. i : int
  842. Returns
  843. -------
  844. scalar (int) or Series (slice, sequence)
  845. """
  846. return self._values[i]
  847. def _slice(self, slobj: slice | np.ndarray, axis: Axis = 0) -> Series:
  848. # axis kwarg is retained for compat with NDFrame method
  849. # _slice is *always* positional
  850. return self._get_values(slobj)
  851. def __getitem__(self, key):
  852. check_dict_or_set_indexers(key)
  853. key = com.apply_if_callable(key, self)
  854. if key is Ellipsis:
  855. return self
  856. key_is_scalar = is_scalar(key)
  857. if isinstance(key, (list, tuple)):
  858. key = unpack_1tuple(key)
  859. if is_integer(key) and self.index._should_fallback_to_positional:
  860. return self._values[key]
  861. elif key_is_scalar:
  862. return self._get_value(key)
  863. if is_hashable(key):
  864. # Otherwise index.get_value will raise InvalidIndexError
  865. try:
  866. # For labels that don't resolve as scalars like tuples and frozensets
  867. result = self._get_value(key)
  868. return result
  869. except (KeyError, TypeError, InvalidIndexError):
  870. # InvalidIndexError for e.g. generator
  871. # see test_series_getitem_corner_generator
  872. if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
  873. # We still have the corner case where a tuple is a key
  874. # in the first level of our MultiIndex
  875. return self._get_values_tuple(key)
  876. if is_iterator(key):
  877. key = list(key)
  878. if com.is_bool_indexer(key):
  879. key = check_bool_indexer(self.index, key)
  880. key = np.asarray(key, dtype=bool)
  881. return self._get_values(key)
  882. return self._get_with(key)
  883. def _get_with(self, key):
  884. # other: fancy integer or otherwise
  885. if isinstance(key, slice):
  886. # _convert_slice_indexer to determine if this slice is positional
  887. # or label based, and if the latter, convert to positional
  888. slobj = self.index._convert_slice_indexer(key, kind="getitem")
  889. return self._slice(slobj)
  890. elif isinstance(key, ABCDataFrame):
  891. raise TypeError(
  892. "Indexing a Series with DataFrame is not "
  893. "supported, use the appropriate DataFrame column"
  894. )
  895. elif isinstance(key, tuple):
  896. return self._get_values_tuple(key)
  897. elif not is_list_like(key):
  898. # e.g. scalars that aren't recognized by lib.is_scalar, GH#32684
  899. return self.loc[key]
  900. if not isinstance(key, (list, np.ndarray, ExtensionArray, Series, Index)):
  901. key = list(key)
  902. if isinstance(key, Index):
  903. key_type = key.inferred_type
  904. else:
  905. key_type = lib.infer_dtype(key, skipna=False)
  906. # Note: The key_type == "boolean" case should be caught by the
  907. # com.is_bool_indexer check in __getitem__
  908. if key_type == "integer":
  909. # We need to decide whether to treat this as a positional indexer
  910. # (i.e. self.iloc) or label-based (i.e. self.loc)
  911. if not self.index._should_fallback_to_positional:
  912. return self.loc[key]
  913. else:
  914. return self.iloc[key]
  915. # handle the dup indexing case GH#4246
  916. return self.loc[key]
  917. def _get_values_tuple(self, key: tuple):
  918. # mpl hackaround
  919. if com.any_none(*key):
  920. # mpl compat if we look up e.g. ser[:, np.newaxis];
  921. # see tests.series.timeseries.test_mpl_compat_hack
  922. # the asarray is needed to avoid returning a 2D DatetimeArray
  923. result = np.asarray(self._values[key])
  924. disallow_ndim_indexing(result)
  925. return result
  926. if not isinstance(self.index, MultiIndex):
  927. raise KeyError("key of type tuple not found and not a MultiIndex")
  928. # If key is contained, would have returned by now
  929. indexer, new_index = self.index.get_loc_level(key)
  930. new_ser = self._constructor(self._values[indexer], index=new_index, copy=False)
  931. if using_copy_on_write() and isinstance(indexer, slice):
  932. new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
  933. return new_ser.__finalize__(self)
  934. def _get_values(self, indexer: slice | npt.NDArray[np.bool_]) -> Series:
  935. new_mgr = self._mgr.getitem_mgr(indexer)
  936. return self._constructor(new_mgr).__finalize__(self)
  937. def _get_value(self, label, takeable: bool = False):
  938. """
  939. Quickly retrieve single value at passed index label.
  940. Parameters
  941. ----------
  942. label : object
  943. takeable : interpret the index as indexers, default False
  944. Returns
  945. -------
  946. scalar value
  947. """
  948. if takeable:
  949. return self._values[label]
  950. # Similar to Index.get_value, but we do not fall back to positional
  951. loc = self.index.get_loc(label)
  952. if is_integer(loc):
  953. return self._values[loc]
  954. if isinstance(self.index, MultiIndex):
  955. mi = self.index
  956. new_values = self._values[loc]
  957. if len(new_values) == 1 and mi.nlevels == 1:
  958. # If more than one level left, we can not return a scalar
  959. return new_values[0]
  960. new_index = mi[loc]
  961. new_index = maybe_droplevels(new_index, label)
  962. new_ser = self._constructor(
  963. new_values, index=new_index, name=self.name, copy=False
  964. )
  965. if using_copy_on_write() and isinstance(loc, slice):
  966. new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
  967. return new_ser.__finalize__(self)
  968. else:
  969. return self.iloc[loc]
  970. def __setitem__(self, key, value) -> None:
  971. if not PYPY and using_copy_on_write():
  972. if sys.getrefcount(self) <= 3:
  973. warnings.warn(
  974. _chained_assignment_msg, ChainedAssignmentError, stacklevel=2
  975. )
  976. check_dict_or_set_indexers(key)
  977. key = com.apply_if_callable(key, self)
  978. cacher_needs_updating = self._check_is_chained_assignment_possible()
  979. if key is Ellipsis:
  980. key = slice(None)
  981. if isinstance(key, slice):
  982. indexer = self.index._convert_slice_indexer(key, kind="getitem")
  983. return self._set_values(indexer, value)
  984. try:
  985. self._set_with_engine(key, value)
  986. except KeyError:
  987. # We have a scalar (or for MultiIndex or object-dtype, scalar-like)
  988. # key that is not present in self.index.
  989. if is_integer(key):
  990. if not self.index._should_fallback_to_positional:
  991. # GH#33469
  992. self.loc[key] = value
  993. else:
  994. # positional setter
  995. # can't use _mgr.setitem_inplace yet bc could have *both*
  996. # KeyError and then ValueError, xref GH#45070
  997. self._set_values(key, value)
  998. else:
  999. # GH#12862 adding a new key to the Series
  1000. self.loc[key] = value
  1001. except (TypeError, ValueError, LossySetitemError):
  1002. # The key was OK, but we cannot set the value losslessly
  1003. indexer = self.index.get_loc(key)
  1004. self._set_values(indexer, value)
  1005. except InvalidIndexError as err:
  1006. if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
  1007. # cases with MultiIndex don't get here bc they raise KeyError
  1008. # e.g. test_basic_getitem_setitem_corner
  1009. raise KeyError(
  1010. "key of type tuple not found and not a MultiIndex"
  1011. ) from err
  1012. if com.is_bool_indexer(key):
  1013. key = check_bool_indexer(self.index, key)
  1014. key = np.asarray(key, dtype=bool)
  1015. if (
  1016. is_list_like(value)
  1017. and len(value) != len(self)
  1018. and not isinstance(value, Series)
  1019. and not is_object_dtype(self.dtype)
  1020. ):
  1021. # Series will be reindexed to have matching length inside
  1022. # _where call below
  1023. # GH#44265
  1024. indexer = key.nonzero()[0]
  1025. self._set_values(indexer, value)
  1026. return
  1027. # otherwise with listlike other we interpret series[mask] = other
  1028. # as series[mask] = other[mask]
  1029. try:
  1030. self._where(~key, value, inplace=True)
  1031. except InvalidIndexError:
  1032. # test_where_dups
  1033. self.iloc[key] = value
  1034. return
  1035. else:
  1036. self._set_with(key, value)
  1037. if cacher_needs_updating:
  1038. self._maybe_update_cacher(inplace=True)
  1039. def _set_with_engine(self, key, value) -> None:
  1040. loc = self.index.get_loc(key)
  1041. # this is equivalent to self._values[key] = value
  1042. self._mgr.setitem_inplace(loc, value)
  1043. def _set_with(self, key, value) -> None:
  1044. # We got here via exception-handling off of InvalidIndexError, so
  1045. # key should always be listlike at this point.
  1046. assert not isinstance(key, tuple)
  1047. if is_iterator(key):
  1048. # Without this, the call to infer_dtype will consume the generator
  1049. key = list(key)
  1050. if not self.index._should_fallback_to_positional:
  1051. # Regardless of the key type, we're treating it as labels
  1052. self._set_labels(key, value)
  1053. else:
  1054. # Note: key_type == "boolean" should not occur because that
  1055. # should be caught by the is_bool_indexer check in __setitem__
  1056. key_type = lib.infer_dtype(key, skipna=False)
  1057. if key_type == "integer":
  1058. self._set_values(key, value)
  1059. else:
  1060. self._set_labels(key, value)
  1061. def _set_labels(self, key, value) -> None:
  1062. key = com.asarray_tuplesafe(key)
  1063. indexer: np.ndarray = self.index.get_indexer(key)
  1064. mask = indexer == -1
  1065. if mask.any():
  1066. raise KeyError(f"{key[mask]} not in index")
  1067. self._set_values(indexer, value)
  1068. def _set_values(self, key, value) -> None:
  1069. if isinstance(key, (Index, Series)):
  1070. key = key._values
  1071. self._mgr = self._mgr.setitem(indexer=key, value=value)
  1072. self._maybe_update_cacher()
  1073. def _set_value(self, label, value, takeable: bool = False) -> None:
  1074. """
  1075. Quickly set single value at passed label.
  1076. If label is not contained, a new object is created with the label
  1077. placed at the end of the result index.
  1078. Parameters
  1079. ----------
  1080. label : object
  1081. Partial indexing with MultiIndex not allowed.
  1082. value : object
  1083. Scalar value.
  1084. takeable : interpret the index as indexers, default False
  1085. """
  1086. if not takeable:
  1087. try:
  1088. loc = self.index.get_loc(label)
  1089. except KeyError:
  1090. # set using a non-recursive method
  1091. self.loc[label] = value
  1092. return
  1093. else:
  1094. loc = label
  1095. self._set_values(loc, value)
  1096. # ----------------------------------------------------------------------
  1097. # Lookup Caching
  1098. @property
  1099. def _is_cached(self) -> bool:
  1100. """Return boolean indicating if self is cached or not."""
  1101. return getattr(self, "_cacher", None) is not None
  1102. def _get_cacher(self):
  1103. """return my cacher or None"""
  1104. cacher = getattr(self, "_cacher", None)
  1105. if cacher is not None:
  1106. cacher = cacher[1]()
  1107. return cacher
  1108. def _reset_cacher(self) -> None:
  1109. """
  1110. Reset the cacher.
  1111. """
  1112. if hasattr(self, "_cacher"):
  1113. del self._cacher
  1114. def _set_as_cached(self, item, cacher) -> None:
  1115. """
  1116. Set the _cacher attribute on the calling object with a weakref to
  1117. cacher.
  1118. """
  1119. if using_copy_on_write():
  1120. return
  1121. self._cacher = (item, weakref.ref(cacher))
  1122. def _clear_item_cache(self) -> None:
  1123. # no-op for Series
  1124. pass
  1125. def _check_is_chained_assignment_possible(self) -> bool:
  1126. """
  1127. See NDFrame._check_is_chained_assignment_possible.__doc__
  1128. """
  1129. if self._is_view and self._is_cached:
  1130. ref = self._get_cacher()
  1131. if ref is not None and ref._is_mixed_type:
  1132. self._check_setitem_copy(t="referent", force=True)
  1133. return True
  1134. return super()._check_is_chained_assignment_possible()
  1135. def _maybe_update_cacher(
  1136. self, clear: bool = False, verify_is_copy: bool = True, inplace: bool = False
  1137. ) -> None:
  1138. """
  1139. See NDFrame._maybe_update_cacher.__doc__
  1140. """
  1141. # for CoW, we never want to update the parent DataFrame cache
  1142. # if the Series changed, but don't keep track of any cacher
  1143. if using_copy_on_write():
  1144. return
  1145. cacher = getattr(self, "_cacher", None)
  1146. if cacher is not None:
  1147. assert self.ndim == 1
  1148. ref: DataFrame = cacher[1]()
  1149. # we are trying to reference a dead referent, hence
  1150. # a copy
  1151. if ref is None:
  1152. del self._cacher
  1153. elif len(self) == len(ref) and self.name in ref.columns:
  1154. # GH#42530 self.name must be in ref.columns
  1155. # to ensure column still in dataframe
  1156. # otherwise, either self or ref has swapped in new arrays
  1157. ref._maybe_cache_changed(cacher[0], self, inplace=inplace)
  1158. else:
  1159. # GH#33675 we have swapped in a new array, so parent
  1160. # reference to self is now invalid
  1161. ref._item_cache.pop(cacher[0], None)
  1162. super()._maybe_update_cacher(
  1163. clear=clear, verify_is_copy=verify_is_copy, inplace=inplace
  1164. )
  1165. # ----------------------------------------------------------------------
  1166. # Unsorted
  1167. @property
  1168. def _is_mixed_type(self) -> bool:
  1169. return False
  1170. def repeat(self, repeats: int | Sequence[int], axis: None = None) -> Series:
  1171. """
  1172. Repeat elements of a Series.
  1173. Returns a new Series where each element of the current Series
  1174. is repeated consecutively a given number of times.
  1175. Parameters
  1176. ----------
  1177. repeats : int or array of ints
  1178. The number of repetitions for each element. This should be a
  1179. non-negative integer. Repeating 0 times will return an empty
  1180. Series.
  1181. axis : None
  1182. Unused. Parameter needed for compatibility with DataFrame.
  1183. Returns
  1184. -------
  1185. Series
  1186. Newly created Series with repeated elements.
  1187. See Also
  1188. --------
  1189. Index.repeat : Equivalent function for Index.
  1190. numpy.repeat : Similar method for :class:`numpy.ndarray`.
  1191. Examples
  1192. --------
  1193. >>> s = pd.Series(['a', 'b', 'c'])
  1194. >>> s
  1195. 0 a
  1196. 1 b
  1197. 2 c
  1198. dtype: object
  1199. >>> s.repeat(2)
  1200. 0 a
  1201. 0 a
  1202. 1 b
  1203. 1 b
  1204. 2 c
  1205. 2 c
  1206. dtype: object
  1207. >>> s.repeat([1, 2, 3])
  1208. 0 a
  1209. 1 b
  1210. 1 b
  1211. 2 c
  1212. 2 c
  1213. 2 c
  1214. dtype: object
  1215. """
  1216. nv.validate_repeat((), {"axis": axis})
  1217. new_index = self.index.repeat(repeats)
  1218. new_values = self._values.repeat(repeats)
  1219. return self._constructor(new_values, index=new_index, copy=False).__finalize__(
  1220. self, method="repeat"
  1221. )
  1222. @overload
  1223. def reset_index(
  1224. self,
  1225. level: IndexLabel = ...,
  1226. *,
  1227. drop: Literal[False] = ...,
  1228. name: Level = ...,
  1229. inplace: Literal[False] = ...,
  1230. allow_duplicates: bool = ...,
  1231. ) -> DataFrame:
  1232. ...
  1233. @overload
  1234. def reset_index(
  1235. self,
  1236. level: IndexLabel = ...,
  1237. *,
  1238. drop: Literal[True],
  1239. name: Level = ...,
  1240. inplace: Literal[False] = ...,
  1241. allow_duplicates: bool = ...,
  1242. ) -> Series:
  1243. ...
  1244. @overload
  1245. def reset_index(
  1246. self,
  1247. level: IndexLabel = ...,
  1248. *,
  1249. drop: bool = ...,
  1250. name: Level = ...,
  1251. inplace: Literal[True],
  1252. allow_duplicates: bool = ...,
  1253. ) -> None:
  1254. ...
  1255. def reset_index(
  1256. self,
  1257. level: IndexLabel = None,
  1258. *,
  1259. drop: bool = False,
  1260. name: Level = lib.no_default,
  1261. inplace: bool = False,
  1262. allow_duplicates: bool = False,
  1263. ) -> DataFrame | Series | None:
  1264. """
  1265. Generate a new DataFrame or Series with the index reset.
  1266. This is useful when the index needs to be treated as a column, or
  1267. when the index is meaningless and needs to be reset to the default
  1268. before another operation.
  1269. Parameters
  1270. ----------
  1271. level : int, str, tuple, or list, default optional
  1272. For a Series with a MultiIndex, only remove the specified levels
  1273. from the index. Removes all levels by default.
  1274. drop : bool, default False
  1275. Just reset the index, without inserting it as a column in
  1276. the new DataFrame.
  1277. name : object, optional
  1278. The name to use for the column containing the original Series
  1279. values. Uses ``self.name`` by default. This argument is ignored
  1280. when `drop` is True.
  1281. inplace : bool, default False
  1282. Modify the Series in place (do not create a new object).
  1283. allow_duplicates : bool, default False
  1284. Allow duplicate column labels to be created.
  1285. .. versionadded:: 1.5.0
  1286. Returns
  1287. -------
  1288. Series or DataFrame or None
  1289. When `drop` is False (the default), a DataFrame is returned.
  1290. The newly created columns will come first in the DataFrame,
  1291. followed by the original Series values.
  1292. When `drop` is True, a `Series` is returned.
  1293. In either case, if ``inplace=True``, no value is returned.
  1294. See Also
  1295. --------
  1296. DataFrame.reset_index: Analogous function for DataFrame.
  1297. Examples
  1298. --------
  1299. >>> s = pd.Series([1, 2, 3, 4], name='foo',
  1300. ... index=pd.Index(['a', 'b', 'c', 'd'], name='idx'))
  1301. Generate a DataFrame with default index.
  1302. >>> s.reset_index()
  1303. idx foo
  1304. 0 a 1
  1305. 1 b 2
  1306. 2 c 3
  1307. 3 d 4
  1308. To specify the name of the new column use `name`.
  1309. >>> s.reset_index(name='values')
  1310. idx values
  1311. 0 a 1
  1312. 1 b 2
  1313. 2 c 3
  1314. 3 d 4
  1315. To generate a new Series with the default set `drop` to True.
  1316. >>> s.reset_index(drop=True)
  1317. 0 1
  1318. 1 2
  1319. 2 3
  1320. 3 4
  1321. Name: foo, dtype: int64
  1322. The `level` parameter is interesting for Series with a multi-level
  1323. index.
  1324. >>> arrays = [np.array(['bar', 'bar', 'baz', 'baz']),
  1325. ... np.array(['one', 'two', 'one', 'two'])]
  1326. >>> s2 = pd.Series(
  1327. ... range(4), name='foo',
  1328. ... index=pd.MultiIndex.from_arrays(arrays,
  1329. ... names=['a', 'b']))
  1330. To remove a specific level from the Index, use `level`.
  1331. >>> s2.reset_index(level='a')
  1332. a foo
  1333. b
  1334. one bar 0
  1335. two bar 1
  1336. one baz 2
  1337. two baz 3
  1338. If `level` is not set, all levels are removed from the Index.
  1339. >>> s2.reset_index()
  1340. a b foo
  1341. 0 bar one 0
  1342. 1 bar two 1
  1343. 2 baz one 2
  1344. 3 baz two 3
  1345. """
  1346. inplace = validate_bool_kwarg(inplace, "inplace")
  1347. if drop:
  1348. new_index = default_index(len(self))
  1349. if level is not None:
  1350. level_list: Sequence[Hashable]
  1351. if not isinstance(level, (tuple, list)):
  1352. level_list = [level]
  1353. else:
  1354. level_list = level
  1355. level_list = [self.index._get_level_number(lev) for lev in level_list]
  1356. if len(level_list) < self.index.nlevels:
  1357. new_index = self.index.droplevel(level_list)
  1358. if inplace:
  1359. self.index = new_index
  1360. elif using_copy_on_write():
  1361. new_ser = self.copy(deep=False)
  1362. new_ser.index = new_index
  1363. return new_ser.__finalize__(self, method="reset_index")
  1364. else:
  1365. return self._constructor(
  1366. self._values.copy(), index=new_index, copy=False
  1367. ).__finalize__(self, method="reset_index")
  1368. elif inplace:
  1369. raise TypeError(
  1370. "Cannot reset_index inplace on a Series to create a DataFrame"
  1371. )
  1372. else:
  1373. if name is lib.no_default:
  1374. # For backwards compatibility, keep columns as [0] instead of
  1375. # [None] when self.name is None
  1376. if self.name is None:
  1377. name = 0
  1378. else:
  1379. name = self.name
  1380. df = self.to_frame(name)
  1381. return df.reset_index(
  1382. level=level, drop=drop, allow_duplicates=allow_duplicates
  1383. )
  1384. return None
  1385. # ----------------------------------------------------------------------
  1386. # Rendering Methods
  1387. def __repr__(self) -> str:
  1388. """
  1389. Return a string representation for a particular Series.
  1390. """
  1391. # pylint: disable=invalid-repr-returned
  1392. repr_params = fmt.get_series_repr_params()
  1393. return self.to_string(**repr_params)
  1394. @overload
  1395. def to_string(
  1396. self,
  1397. buf: None = ...,
  1398. na_rep: str = ...,
  1399. float_format: str | None = ...,
  1400. header: bool = ...,
  1401. index: bool = ...,
  1402. length=...,
  1403. dtype=...,
  1404. name=...,
  1405. max_rows: int | None = ...,
  1406. min_rows: int | None = ...,
  1407. ) -> str:
  1408. ...
  1409. @overload
  1410. def to_string(
  1411. self,
  1412. buf: FilePath | WriteBuffer[str],
  1413. na_rep: str = ...,
  1414. float_format: str | None = ...,
  1415. header: bool = ...,
  1416. index: bool = ...,
  1417. length=...,
  1418. dtype=...,
  1419. name=...,
  1420. max_rows: int | None = ...,
  1421. min_rows: int | None = ...,
  1422. ) -> None:
  1423. ...
  1424. def to_string(
  1425. self,
  1426. buf: FilePath | WriteBuffer[str] | None = None,
  1427. na_rep: str = "NaN",
  1428. float_format: str | None = None,
  1429. header: bool = True,
  1430. index: bool = True,
  1431. length: bool = False,
  1432. dtype: bool = False,
  1433. name: bool = False,
  1434. max_rows: int | None = None,
  1435. min_rows: int | None = None,
  1436. ) -> str | None:
  1437. """
  1438. Render a string representation of the Series.
  1439. Parameters
  1440. ----------
  1441. buf : StringIO-like, optional
  1442. Buffer to write to.
  1443. na_rep : str, optional
  1444. String representation of NaN to use, default 'NaN'.
  1445. float_format : one-parameter function, optional
  1446. Formatter function to apply to columns' elements if they are
  1447. floats, default None.
  1448. header : bool, default True
  1449. Add the Series header (index name).
  1450. index : bool, optional
  1451. Add index (row) labels, default True.
  1452. length : bool, default False
  1453. Add the Series length.
  1454. dtype : bool, default False
  1455. Add the Series dtype.
  1456. name : bool, default False
  1457. Add the Series name if not None.
  1458. max_rows : int, optional
  1459. Maximum number of rows to show before truncating. If None, show
  1460. all.
  1461. min_rows : int, optional
  1462. The number of rows to display in a truncated repr (when number
  1463. of rows is above `max_rows`).
  1464. Returns
  1465. -------
  1466. str or None
  1467. String representation of Series if ``buf=None``, otherwise None.
  1468. """
  1469. formatter = fmt.SeriesFormatter(
  1470. self,
  1471. name=name,
  1472. length=length,
  1473. header=header,
  1474. index=index,
  1475. dtype=dtype,
  1476. na_rep=na_rep,
  1477. float_format=float_format,
  1478. min_rows=min_rows,
  1479. max_rows=max_rows,
  1480. )
  1481. result = formatter.to_string()
  1482. # catch contract violations
  1483. if not isinstance(result, str):
  1484. raise AssertionError(
  1485. "result must be of type str, type "
  1486. f"of result is {repr(type(result).__name__)}"
  1487. )
  1488. if buf is None:
  1489. return result
  1490. else:
  1491. if hasattr(buf, "write"):
  1492. buf.write(result)
  1493. else:
  1494. with open(buf, "w") as f:
  1495. f.write(result)
  1496. return None
  1497. @doc(
  1498. klass=_shared_doc_kwargs["klass"],
  1499. storage_options=_shared_docs["storage_options"],
  1500. examples=dedent(
  1501. """Examples
  1502. --------
  1503. >>> s = pd.Series(["elk", "pig", "dog", "quetzal"], name="animal")
  1504. >>> print(s.to_markdown())
  1505. | | animal |
  1506. |---:|:---------|
  1507. | 0 | elk |
  1508. | 1 | pig |
  1509. | 2 | dog |
  1510. | 3 | quetzal |
  1511. Output markdown with a tabulate option.
  1512. >>> print(s.to_markdown(tablefmt="grid"))
  1513. +----+----------+
  1514. | | animal |
  1515. +====+==========+
  1516. | 0 | elk |
  1517. +----+----------+
  1518. | 1 | pig |
  1519. +----+----------+
  1520. | 2 | dog |
  1521. +----+----------+
  1522. | 3 | quetzal |
  1523. +----+----------+"""
  1524. ),
  1525. )
  1526. def to_markdown(
  1527. self,
  1528. buf: IO[str] | None = None,
  1529. mode: str = "wt",
  1530. index: bool = True,
  1531. storage_options: StorageOptions = None,
  1532. **kwargs,
  1533. ) -> str | None:
  1534. """
  1535. Print {klass} in Markdown-friendly format.
  1536. Parameters
  1537. ----------
  1538. buf : str, Path or StringIO-like, optional, default None
  1539. Buffer to write to. If None, the output is returned as a string.
  1540. mode : str, optional
  1541. Mode in which file is opened, "wt" by default.
  1542. index : bool, optional, default True
  1543. Add index (row) labels.
  1544. .. versionadded:: 1.1.0
  1545. {storage_options}
  1546. .. versionadded:: 1.2.0
  1547. **kwargs
  1548. These parameters will be passed to `tabulate \
  1549. <https://pypi.org/project/tabulate>`_.
  1550. Returns
  1551. -------
  1552. str
  1553. {klass} in Markdown-friendly format.
  1554. Notes
  1555. -----
  1556. Requires the `tabulate <https://pypi.org/project/tabulate>`_ package.
  1557. {examples}
  1558. """
  1559. return self.to_frame().to_markdown(
  1560. buf, mode, index, storage_options=storage_options, **kwargs
  1561. )
  1562. # ----------------------------------------------------------------------
  1563. def items(self) -> Iterable[tuple[Hashable, Any]]:
  1564. """
  1565. Lazily iterate over (index, value) tuples.
  1566. This method returns an iterable tuple (index, value). This is
  1567. convenient if you want to create a lazy iterator.
  1568. Returns
  1569. -------
  1570. iterable
  1571. Iterable of tuples containing the (index, value) pairs from a
  1572. Series.
  1573. See Also
  1574. --------
  1575. DataFrame.items : Iterate over (column name, Series) pairs.
  1576. DataFrame.iterrows : Iterate over DataFrame rows as (index, Series) pairs.
  1577. Examples
  1578. --------
  1579. >>> s = pd.Series(['A', 'B', 'C'])
  1580. >>> for index, value in s.items():
  1581. ... print(f"Index : {index}, Value : {value}")
  1582. Index : 0, Value : A
  1583. Index : 1, Value : B
  1584. Index : 2, Value : C
  1585. """
  1586. return zip(iter(self.index), iter(self))
  1587. # ----------------------------------------------------------------------
  1588. # Misc public methods
  1589. def keys(self) -> Index:
  1590. """
  1591. Return alias for index.
  1592. Returns
  1593. -------
  1594. Index
  1595. Index of the Series.
  1596. """
  1597. return self.index
  1598. def to_dict(self, into: type[dict] = dict) -> dict:
  1599. """
  1600. Convert Series to {label -> value} dict or dict-like object.
  1601. Parameters
  1602. ----------
  1603. into : class, default dict
  1604. The collections.abc.Mapping subclass to use as the return
  1605. object. Can be the actual class or an empty
  1606. instance of the mapping type you want. If you want a
  1607. collections.defaultdict, you must pass it initialized.
  1608. Returns
  1609. -------
  1610. collections.abc.Mapping
  1611. Key-value representation of Series.
  1612. Examples
  1613. --------
  1614. >>> s = pd.Series([1, 2, 3, 4])
  1615. >>> s.to_dict()
  1616. {0: 1, 1: 2, 2: 3, 3: 4}
  1617. >>> from collections import OrderedDict, defaultdict
  1618. >>> s.to_dict(OrderedDict)
  1619. OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)])
  1620. >>> dd = defaultdict(list)
  1621. >>> s.to_dict(dd)
  1622. defaultdict(<class 'list'>, {0: 1, 1: 2, 2: 3, 3: 4})
  1623. """
  1624. # GH16122
  1625. into_c = com.standardize_mapping(into)
  1626. if is_object_dtype(self) or is_extension_array_dtype(self):
  1627. return into_c((k, maybe_box_native(v)) for k, v in self.items())
  1628. else:
  1629. # Not an object dtype => all types will be the same so let the default
  1630. # indexer return native python type
  1631. return into_c(self.items())
  1632. def to_frame(self, name: Hashable = lib.no_default) -> DataFrame:
  1633. """
  1634. Convert Series to DataFrame.
  1635. Parameters
  1636. ----------
  1637. name : object, optional
  1638. The passed name should substitute for the series name (if it has
  1639. one).
  1640. Returns
  1641. -------
  1642. DataFrame
  1643. DataFrame representation of Series.
  1644. Examples
  1645. --------
  1646. >>> s = pd.Series(["a", "b", "c"],
  1647. ... name="vals")
  1648. >>> s.to_frame()
  1649. vals
  1650. 0 a
  1651. 1 b
  1652. 2 c
  1653. """
  1654. columns: Index
  1655. if name is lib.no_default:
  1656. name = self.name
  1657. if name is None:
  1658. # default to [0], same as we would get with DataFrame(self)
  1659. columns = default_index(1)
  1660. else:
  1661. columns = Index([name])
  1662. else:
  1663. columns = Index([name])
  1664. mgr = self._mgr.to_2d_mgr(columns)
  1665. df = self._constructor_expanddim(mgr)
  1666. return df.__finalize__(self, method="to_frame")
  1667. def _set_name(
  1668. self, name, inplace: bool = False, deep: bool | None = None
  1669. ) -> Series:
  1670. """
  1671. Set the Series name.
  1672. Parameters
  1673. ----------
  1674. name : str
  1675. inplace : bool
  1676. Whether to modify `self` directly or return a copy.
  1677. deep : bool|None, default None
  1678. Whether to do a deep copy, a shallow copy, or Copy on Write(None)
  1679. """
  1680. inplace = validate_bool_kwarg(inplace, "inplace")
  1681. ser = self if inplace else self.copy(deep and not using_copy_on_write())
  1682. ser.name = name
  1683. return ser
  1684. @Appender(
  1685. """
  1686. Examples
  1687. --------
  1688. >>> ser = pd.Series([390., 350., 30., 20.],
  1689. ... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], name="Max Speed")
  1690. >>> ser
  1691. Falcon 390.0
  1692. Falcon 350.0
  1693. Parrot 30.0
  1694. Parrot 20.0
  1695. Name: Max Speed, dtype: float64
  1696. >>> ser.groupby(["a", "b", "a", "b"]).mean()
  1697. a 210.0
  1698. b 185.0
  1699. Name: Max Speed, dtype: float64
  1700. >>> ser.groupby(level=0).mean()
  1701. Falcon 370.0
  1702. Parrot 25.0
  1703. Name: Max Speed, dtype: float64
  1704. >>> ser.groupby(ser > 100).mean()
  1705. Max Speed
  1706. False 25.0
  1707. True 370.0
  1708. Name: Max Speed, dtype: float64
  1709. **Grouping by Indexes**
  1710. We can groupby different levels of a hierarchical index
  1711. using the `level` parameter:
  1712. >>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'],
  1713. ... ['Captive', 'Wild', 'Captive', 'Wild']]
  1714. >>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))
  1715. >>> ser = pd.Series([390., 350., 30., 20.], index=index, name="Max Speed")
  1716. >>> ser
  1717. Animal Type
  1718. Falcon Captive 390.0
  1719. Wild 350.0
  1720. Parrot Captive 30.0
  1721. Wild 20.0
  1722. Name: Max Speed, dtype: float64
  1723. >>> ser.groupby(level=0).mean()
  1724. Animal
  1725. Falcon 370.0
  1726. Parrot 25.0
  1727. Name: Max Speed, dtype: float64
  1728. >>> ser.groupby(level="Type").mean()
  1729. Type
  1730. Captive 210.0
  1731. Wild 185.0
  1732. Name: Max Speed, dtype: float64
  1733. We can also choose to include `NA` in group keys or not by defining
  1734. `dropna` parameter, the default setting is `True`.
  1735. >>> ser = pd.Series([1, 2, 3, 3], index=["a", 'a', 'b', np.nan])
  1736. >>> ser.groupby(level=0).sum()
  1737. a 3
  1738. b 3
  1739. dtype: int64
  1740. >>> ser.groupby(level=0, dropna=False).sum()
  1741. a 3
  1742. b 3
  1743. NaN 3
  1744. dtype: int64
  1745. >>> arrays = ['Falcon', 'Falcon', 'Parrot', 'Parrot']
  1746. >>> ser = pd.Series([390., 350., 30., 20.], index=arrays, name="Max Speed")
  1747. >>> ser.groupby(["a", "b", "a", np.nan]).mean()
  1748. a 210.0
  1749. b 350.0
  1750. Name: Max Speed, dtype: float64
  1751. >>> ser.groupby(["a", "b", "a", np.nan], dropna=False).mean()
  1752. a 210.0
  1753. b 350.0
  1754. NaN 20.0
  1755. Name: Max Speed, dtype: float64
  1756. """
  1757. )
  1758. @Appender(_shared_docs["groupby"] % _shared_doc_kwargs)
  1759. def groupby(
  1760. self,
  1761. by=None,
  1762. axis: Axis = 0,
  1763. level: IndexLabel = None,
  1764. as_index: bool = True,
  1765. sort: bool = True,
  1766. group_keys: bool = True,
  1767. observed: bool = False,
  1768. dropna: bool = True,
  1769. ) -> SeriesGroupBy:
  1770. from pandas.core.groupby.generic import SeriesGroupBy
  1771. if level is None and by is None:
  1772. raise TypeError("You have to supply one of 'by' and 'level'")
  1773. if not as_index:
  1774. raise TypeError("as_index=False only valid with DataFrame")
  1775. axis = self._get_axis_number(axis)
  1776. return SeriesGroupBy(
  1777. obj=self,
  1778. keys=by,
  1779. axis=axis,
  1780. level=level,
  1781. as_index=as_index,
  1782. sort=sort,
  1783. group_keys=group_keys,
  1784. observed=observed,
  1785. dropna=dropna,
  1786. )
  1787. # ----------------------------------------------------------------------
  1788. # Statistics, overridden ndarray methods
  1789. # TODO: integrate bottleneck
  1790. def count(self):
  1791. """
  1792. Return number of non-NA/null observations in the Series.
  1793. Returns
  1794. -------
  1795. int or Series (if level specified)
  1796. Number of non-null values in the Series.
  1797. See Also
  1798. --------
  1799. DataFrame.count : Count non-NA cells for each column or row.
  1800. Examples
  1801. --------
  1802. >>> s = pd.Series([0.0, 1.0, np.nan])
  1803. >>> s.count()
  1804. 2
  1805. """
  1806. return notna(self._values).sum().astype("int64")
  1807. def mode(self, dropna: bool = True) -> Series:
  1808. """
  1809. Return the mode(s) of the Series.
  1810. The mode is the value that appears most often. There can be multiple modes.
  1811. Always returns Series even if only one value is returned.
  1812. Parameters
  1813. ----------
  1814. dropna : bool, default True
  1815. Don't consider counts of NaN/NaT.
  1816. Returns
  1817. -------
  1818. Series
  1819. Modes of the Series in sorted order.
  1820. """
  1821. # TODO: Add option for bins like value_counts()
  1822. values = self._values
  1823. if isinstance(values, np.ndarray):
  1824. res_values = algorithms.mode(values, dropna=dropna)
  1825. else:
  1826. res_values = values._mode(dropna=dropna)
  1827. # Ensure index is type stable (should always use int index)
  1828. return self._constructor(
  1829. res_values, index=range(len(res_values)), name=self.name, copy=False
  1830. )
  1831. def unique(self) -> ArrayLike: # pylint: disable=useless-parent-delegation
  1832. """
  1833. Return unique values of Series object.
  1834. Uniques are returned in order of appearance. Hash table-based unique,
  1835. therefore does NOT sort.
  1836. Returns
  1837. -------
  1838. ndarray or ExtensionArray
  1839. The unique values returned as a NumPy array. See Notes.
  1840. See Also
  1841. --------
  1842. Series.drop_duplicates : Return Series with duplicate values removed.
  1843. unique : Top-level unique method for any 1-d array-like object.
  1844. Index.unique : Return Index with unique values from an Index object.
  1845. Notes
  1846. -----
  1847. Returns the unique values as a NumPy array. In case of an
  1848. extension-array backed Series, a new
  1849. :class:`~api.extensions.ExtensionArray` of that type with just
  1850. the unique values is returned. This includes
  1851. * Categorical
  1852. * Period
  1853. * Datetime with Timezone
  1854. * Datetime without Timezone
  1855. * Timedelta
  1856. * Interval
  1857. * Sparse
  1858. * IntegerNA
  1859. See Examples section.
  1860. Examples
  1861. --------
  1862. >>> pd.Series([2, 1, 3, 3], name='A').unique()
  1863. array([2, 1, 3])
  1864. >>> pd.Series([pd.Timestamp('2016-01-01') for _ in range(3)]).unique()
  1865. <DatetimeArray>
  1866. ['2016-01-01 00:00:00']
  1867. Length: 1, dtype: datetime64[ns]
  1868. >>> pd.Series([pd.Timestamp('2016-01-01', tz='US/Eastern')
  1869. ... for _ in range(3)]).unique()
  1870. <DatetimeArray>
  1871. ['2016-01-01 00:00:00-05:00']
  1872. Length: 1, dtype: datetime64[ns, US/Eastern]
  1873. An Categorical will return categories in the order of
  1874. appearance and with the same dtype.
  1875. >>> pd.Series(pd.Categorical(list('baabc'))).unique()
  1876. ['b', 'a', 'c']
  1877. Categories (3, object): ['a', 'b', 'c']
  1878. >>> pd.Series(pd.Categorical(list('baabc'), categories=list('abc'),
  1879. ... ordered=True)).unique()
  1880. ['b', 'a', 'c']
  1881. Categories (3, object): ['a' < 'b' < 'c']
  1882. """
  1883. return super().unique()
  1884. @overload
  1885. def drop_duplicates(
  1886. self,
  1887. *,
  1888. keep: DropKeep = ...,
  1889. inplace: Literal[False] = ...,
  1890. ignore_index: bool = ...,
  1891. ) -> Series:
  1892. ...
  1893. @overload
  1894. def drop_duplicates(
  1895. self, *, keep: DropKeep = ..., inplace: Literal[True], ignore_index: bool = ...
  1896. ) -> None:
  1897. ...
  1898. @overload
  1899. def drop_duplicates(
  1900. self, *, keep: DropKeep = ..., inplace: bool = ..., ignore_index: bool = ...
  1901. ) -> Series | None:
  1902. ...
  1903. def drop_duplicates(
  1904. self,
  1905. *,
  1906. keep: DropKeep = "first",
  1907. inplace: bool = False,
  1908. ignore_index: bool = False,
  1909. ) -> Series | None:
  1910. """
  1911. Return Series with duplicate values removed.
  1912. Parameters
  1913. ----------
  1914. keep : {'first', 'last', ``False``}, default 'first'
  1915. Method to handle dropping duplicates:
  1916. - 'first' : Drop duplicates except for the first occurrence.
  1917. - 'last' : Drop duplicates except for the last occurrence.
  1918. - ``False`` : Drop all duplicates.
  1919. inplace : bool, default ``False``
  1920. If ``True``, performs operation inplace and returns None.
  1921. ignore_index : bool, default ``False``
  1922. If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.
  1923. .. versionadded:: 2.0.0
  1924. Returns
  1925. -------
  1926. Series or None
  1927. Series with duplicates dropped or None if ``inplace=True``.
  1928. See Also
  1929. --------
  1930. Index.drop_duplicates : Equivalent method on Index.
  1931. DataFrame.drop_duplicates : Equivalent method on DataFrame.
  1932. Series.duplicated : Related method on Series, indicating duplicate
  1933. Series values.
  1934. Series.unique : Return unique values as an array.
  1935. Examples
  1936. --------
  1937. Generate a Series with duplicated entries.
  1938. >>> s = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama', 'hippo'],
  1939. ... name='animal')
  1940. >>> s
  1941. 0 lama
  1942. 1 cow
  1943. 2 lama
  1944. 3 beetle
  1945. 4 lama
  1946. 5 hippo
  1947. Name: animal, dtype: object
  1948. With the 'keep' parameter, the selection behaviour of duplicated values
  1949. can be changed. The value 'first' keeps the first occurrence for each
  1950. set of duplicated entries. The default value of keep is 'first'.
  1951. >>> s.drop_duplicates()
  1952. 0 lama
  1953. 1 cow
  1954. 3 beetle
  1955. 5 hippo
  1956. Name: animal, dtype: object
  1957. The value 'last' for parameter 'keep' keeps the last occurrence for
  1958. each set of duplicated entries.
  1959. >>> s.drop_duplicates(keep='last')
  1960. 1 cow
  1961. 3 beetle
  1962. 4 lama
  1963. 5 hippo
  1964. Name: animal, dtype: object
  1965. The value ``False`` for parameter 'keep' discards all sets of
  1966. duplicated entries.
  1967. >>> s.drop_duplicates(keep=False)
  1968. 1 cow
  1969. 3 beetle
  1970. 5 hippo
  1971. Name: animal, dtype: object
  1972. """
  1973. inplace = validate_bool_kwarg(inplace, "inplace")
  1974. result = super().drop_duplicates(keep=keep)
  1975. if ignore_index:
  1976. result.index = default_index(len(result))
  1977. if inplace:
  1978. self._update_inplace(result)
  1979. return None
  1980. else:
  1981. return result
  1982. def duplicated(self, keep: DropKeep = "first") -> Series:
  1983. """
  1984. Indicate duplicate Series values.
  1985. Duplicated values are indicated as ``True`` values in the resulting
  1986. Series. Either all duplicates, all except the first or all except the
  1987. last occurrence of duplicates can be indicated.
  1988. Parameters
  1989. ----------
  1990. keep : {'first', 'last', False}, default 'first'
  1991. Method to handle dropping duplicates:
  1992. - 'first' : Mark duplicates as ``True`` except for the first
  1993. occurrence.
  1994. - 'last' : Mark duplicates as ``True`` except for the last
  1995. occurrence.
  1996. - ``False`` : Mark all duplicates as ``True``.
  1997. Returns
  1998. -------
  1999. Series[bool]
  2000. Series indicating whether each value has occurred in the
  2001. preceding values.
  2002. See Also
  2003. --------
  2004. Index.duplicated : Equivalent method on pandas.Index.
  2005. DataFrame.duplicated : Equivalent method on pandas.DataFrame.
  2006. Series.drop_duplicates : Remove duplicate values from Series.
  2007. Examples
  2008. --------
  2009. By default, for each set of duplicated values, the first occurrence is
  2010. set on False and all others on True:
  2011. >>> animals = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama'])
  2012. >>> animals.duplicated()
  2013. 0 False
  2014. 1 False
  2015. 2 True
  2016. 3 False
  2017. 4 True
  2018. dtype: bool
  2019. which is equivalent to
  2020. >>> animals.duplicated(keep='first')
  2021. 0 False
  2022. 1 False
  2023. 2 True
  2024. 3 False
  2025. 4 True
  2026. dtype: bool
  2027. By using 'last', the last occurrence of each set of duplicated values
  2028. is set on False and all others on True:
  2029. >>> animals.duplicated(keep='last')
  2030. 0 True
  2031. 1 False
  2032. 2 True
  2033. 3 False
  2034. 4 False
  2035. dtype: bool
  2036. By setting keep on ``False``, all duplicates are True:
  2037. >>> animals.duplicated(keep=False)
  2038. 0 True
  2039. 1 False
  2040. 2 True
  2041. 3 False
  2042. 4 True
  2043. dtype: bool
  2044. """
  2045. res = self._duplicated(keep=keep)
  2046. result = self._constructor(res, index=self.index, copy=False)
  2047. return result.__finalize__(self, method="duplicated")
  2048. def idxmin(self, axis: Axis = 0, skipna: bool = True, *args, **kwargs) -> Hashable:
  2049. """
  2050. Return the row label of the minimum value.
  2051. If multiple values equal the minimum, the first row label with that
  2052. value is returned.
  2053. Parameters
  2054. ----------
  2055. axis : {0 or 'index'}
  2056. Unused. Parameter needed for compatibility with DataFrame.
  2057. skipna : bool, default True
  2058. Exclude NA/null values. If the entire Series is NA, the result
  2059. will be NA.
  2060. *args, **kwargs
  2061. Additional arguments and keywords have no effect but might be
  2062. accepted for compatibility with NumPy.
  2063. Returns
  2064. -------
  2065. Index
  2066. Label of the minimum value.
  2067. Raises
  2068. ------
  2069. ValueError
  2070. If the Series is empty.
  2071. See Also
  2072. --------
  2073. numpy.argmin : Return indices of the minimum values
  2074. along the given axis.
  2075. DataFrame.idxmin : Return index of first occurrence of minimum
  2076. over requested axis.
  2077. Series.idxmax : Return index *label* of the first occurrence
  2078. of maximum of values.
  2079. Notes
  2080. -----
  2081. This method is the Series version of ``ndarray.argmin``. This method
  2082. returns the label of the minimum, while ``ndarray.argmin`` returns
  2083. the position. To get the position, use ``series.values.argmin()``.
  2084. Examples
  2085. --------
  2086. >>> s = pd.Series(data=[1, None, 4, 1],
  2087. ... index=['A', 'B', 'C', 'D'])
  2088. >>> s
  2089. A 1.0
  2090. B NaN
  2091. C 4.0
  2092. D 1.0
  2093. dtype: float64
  2094. >>> s.idxmin()
  2095. 'A'
  2096. If `skipna` is False and there is an NA value in the data,
  2097. the function returns ``nan``.
  2098. >>> s.idxmin(skipna=False)
  2099. nan
  2100. """
  2101. # error: Argument 1 to "argmin" of "IndexOpsMixin" has incompatible type "Union
  2102. # [int, Literal['index', 'columns']]"; expected "Optional[int]"
  2103. i = self.argmin(axis, skipna, *args, **kwargs) # type: ignore[arg-type]
  2104. if i == -1:
  2105. return np.nan
  2106. return self.index[i]
  2107. def idxmax(self, axis: Axis = 0, skipna: bool = True, *args, **kwargs) -> Hashable:
  2108. """
  2109. Return the row label of the maximum value.
  2110. If multiple values equal the maximum, the first row label with that
  2111. value is returned.
  2112. Parameters
  2113. ----------
  2114. axis : {0 or 'index'}
  2115. Unused. Parameter needed for compatibility with DataFrame.
  2116. skipna : bool, default True
  2117. Exclude NA/null values. If the entire Series is NA, the result
  2118. will be NA.
  2119. *args, **kwargs
  2120. Additional arguments and keywords have no effect but might be
  2121. accepted for compatibility with NumPy.
  2122. Returns
  2123. -------
  2124. Index
  2125. Label of the maximum value.
  2126. Raises
  2127. ------
  2128. ValueError
  2129. If the Series is empty.
  2130. See Also
  2131. --------
  2132. numpy.argmax : Return indices of the maximum values
  2133. along the given axis.
  2134. DataFrame.idxmax : Return index of first occurrence of maximum
  2135. over requested axis.
  2136. Series.idxmin : Return index *label* of the first occurrence
  2137. of minimum of values.
  2138. Notes
  2139. -----
  2140. This method is the Series version of ``ndarray.argmax``. This method
  2141. returns the label of the maximum, while ``ndarray.argmax`` returns
  2142. the position. To get the position, use ``series.values.argmax()``.
  2143. Examples
  2144. --------
  2145. >>> s = pd.Series(data=[1, None, 4, 3, 4],
  2146. ... index=['A', 'B', 'C', 'D', 'E'])
  2147. >>> s
  2148. A 1.0
  2149. B NaN
  2150. C 4.0
  2151. D 3.0
  2152. E 4.0
  2153. dtype: float64
  2154. >>> s.idxmax()
  2155. 'C'
  2156. If `skipna` is False and there is an NA value in the data,
  2157. the function returns ``nan``.
  2158. >>> s.idxmax(skipna=False)
  2159. nan
  2160. """
  2161. # error: Argument 1 to "argmax" of "IndexOpsMixin" has incompatible type
  2162. # "Union[int, Literal['index', 'columns']]"; expected "Optional[int]"
  2163. i = self.argmax(axis, skipna, *args, **kwargs) # type: ignore[arg-type]
  2164. if i == -1:
  2165. return np.nan
  2166. return self.index[i]
  2167. def round(self, decimals: int = 0, *args, **kwargs) -> Series:
  2168. """
  2169. Round each value in a Series to the given number of decimals.
  2170. Parameters
  2171. ----------
  2172. decimals : int, default 0
  2173. Number of decimal places to round to. If decimals is negative,
  2174. it specifies the number of positions to the left of the decimal point.
  2175. *args, **kwargs
  2176. Additional arguments and keywords have no effect but might be
  2177. accepted for compatibility with NumPy.
  2178. Returns
  2179. -------
  2180. Series
  2181. Rounded values of the Series.
  2182. See Also
  2183. --------
  2184. numpy.around : Round values of an np.array.
  2185. DataFrame.round : Round values of a DataFrame.
  2186. Examples
  2187. --------
  2188. >>> s = pd.Series([0.1, 1.3, 2.7])
  2189. >>> s.round()
  2190. 0 0.0
  2191. 1 1.0
  2192. 2 3.0
  2193. dtype: float64
  2194. """
  2195. nv.validate_round(args, kwargs)
  2196. result = self._values.round(decimals)
  2197. result = self._constructor(result, index=self.index, copy=False).__finalize__(
  2198. self, method="round"
  2199. )
  2200. return result
  2201. @overload
  2202. def quantile(
  2203. self, q: float = ..., interpolation: QuantileInterpolation = ...
  2204. ) -> float:
  2205. ...
  2206. @overload
  2207. def quantile(
  2208. self,
  2209. q: Sequence[float] | AnyArrayLike,
  2210. interpolation: QuantileInterpolation = ...,
  2211. ) -> Series:
  2212. ...
  2213. @overload
  2214. def quantile(
  2215. self,
  2216. q: float | Sequence[float] | AnyArrayLike = ...,
  2217. interpolation: QuantileInterpolation = ...,
  2218. ) -> float | Series:
  2219. ...
  2220. def quantile(
  2221. self,
  2222. q: float | Sequence[float] | AnyArrayLike = 0.5,
  2223. interpolation: QuantileInterpolation = "linear",
  2224. ) -> float | Series:
  2225. """
  2226. Return value at the given quantile.
  2227. Parameters
  2228. ----------
  2229. q : float or array-like, default 0.5 (50% quantile)
  2230. The quantile(s) to compute, which can lie in range: 0 <= q <= 1.
  2231. interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
  2232. This optional parameter specifies the interpolation method to use,
  2233. when the desired quantile lies between two data points `i` and `j`:
  2234. * linear: `i + (j - i) * fraction`, where `fraction` is the
  2235. fractional part of the index surrounded by `i` and `j`.
  2236. * lower: `i`.
  2237. * higher: `j`.
  2238. * nearest: `i` or `j` whichever is nearest.
  2239. * midpoint: (`i` + `j`) / 2.
  2240. Returns
  2241. -------
  2242. float or Series
  2243. If ``q`` is an array, a Series will be returned where the
  2244. index is ``q`` and the values are the quantiles, otherwise
  2245. a float will be returned.
  2246. See Also
  2247. --------
  2248. core.window.Rolling.quantile : Calculate the rolling quantile.
  2249. numpy.percentile : Returns the q-th percentile(s) of the array elements.
  2250. Examples
  2251. --------
  2252. >>> s = pd.Series([1, 2, 3, 4])
  2253. >>> s.quantile(.5)
  2254. 2.5
  2255. >>> s.quantile([.25, .5, .75])
  2256. 0.25 1.75
  2257. 0.50 2.50
  2258. 0.75 3.25
  2259. dtype: float64
  2260. """
  2261. validate_percentile(q)
  2262. # We dispatch to DataFrame so that core.internals only has to worry
  2263. # about 2D cases.
  2264. df = self.to_frame()
  2265. result = df.quantile(q=q, interpolation=interpolation, numeric_only=False)
  2266. if result.ndim == 2:
  2267. result = result.iloc[:, 0]
  2268. if is_list_like(q):
  2269. result.name = self.name
  2270. idx = Index(q, dtype=np.float64)
  2271. return self._constructor(result, index=idx, name=self.name)
  2272. else:
  2273. # scalar
  2274. return result.iloc[0]
  2275. def corr(
  2276. self,
  2277. other: Series,
  2278. method: CorrelationMethod = "pearson",
  2279. min_periods: int | None = None,
  2280. ) -> float:
  2281. """
  2282. Compute correlation with `other` Series, excluding missing values.
  2283. The two `Series` objects are not required to be the same length and will be
  2284. aligned internally before the correlation function is applied.
  2285. Parameters
  2286. ----------
  2287. other : Series
  2288. Series with which to compute the correlation.
  2289. method : {'pearson', 'kendall', 'spearman'} or callable
  2290. Method used to compute correlation:
  2291. - pearson : Standard correlation coefficient
  2292. - kendall : Kendall Tau correlation coefficient
  2293. - spearman : Spearman rank correlation
  2294. - callable: Callable with input two 1d ndarrays and returning a float.
  2295. .. warning::
  2296. Note that the returned matrix from corr will have 1 along the
  2297. diagonals and will be symmetric regardless of the callable's
  2298. behavior.
  2299. min_periods : int, optional
  2300. Minimum number of observations needed to have a valid result.
  2301. Returns
  2302. -------
  2303. float
  2304. Correlation with other.
  2305. See Also
  2306. --------
  2307. DataFrame.corr : Compute pairwise correlation between columns.
  2308. DataFrame.corrwith : Compute pairwise correlation with another
  2309. DataFrame or Series.
  2310. Notes
  2311. -----
  2312. Pearson, Kendall and Spearman correlation are currently computed using pairwise complete observations.
  2313. * `Pearson correlation coefficient <https://en.wikipedia.org/wiki/Pearson_correlation_coefficient>`_
  2314. * `Kendall rank correlation coefficient <https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient>`_
  2315. * `Spearman's rank correlation coefficient <https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient>`_
  2316. Examples
  2317. --------
  2318. >>> def histogram_intersection(a, b):
  2319. ... v = np.minimum(a, b).sum().round(decimals=1)
  2320. ... return v
  2321. >>> s1 = pd.Series([.2, .0, .6, .2])
  2322. >>> s2 = pd.Series([.3, .6, .0, .1])
  2323. >>> s1.corr(s2, method=histogram_intersection)
  2324. 0.3
  2325. """ # noqa:E501
  2326. this, other = self.align(other, join="inner", copy=False)
  2327. if len(this) == 0:
  2328. return np.nan
  2329. if method in ["pearson", "spearman", "kendall"] or callable(method):
  2330. return nanops.nancorr(
  2331. this.values, other.values, method=method, min_periods=min_periods
  2332. )
  2333. raise ValueError(
  2334. "method must be either 'pearson', "
  2335. "'spearman', 'kendall', or a callable, "
  2336. f"'{method}' was supplied"
  2337. )
  2338. def cov(
  2339. self,
  2340. other: Series,
  2341. min_periods: int | None = None,
  2342. ddof: int | None = 1,
  2343. ) -> float:
  2344. """
  2345. Compute covariance with Series, excluding missing values.
  2346. The two `Series` objects are not required to be the same length and
  2347. will be aligned internally before the covariance is calculated.
  2348. Parameters
  2349. ----------
  2350. other : Series
  2351. Series with which to compute the covariance.
  2352. min_periods : int, optional
  2353. Minimum number of observations needed to have a valid result.
  2354. ddof : int, default 1
  2355. Delta degrees of freedom. The divisor used in calculations
  2356. is ``N - ddof``, where ``N`` represents the number of elements.
  2357. .. versionadded:: 1.1.0
  2358. Returns
  2359. -------
  2360. float
  2361. Covariance between Series and other normalized by N-1
  2362. (unbiased estimator).
  2363. See Also
  2364. --------
  2365. DataFrame.cov : Compute pairwise covariance of columns.
  2366. Examples
  2367. --------
  2368. >>> s1 = pd.Series([0.90010907, 0.13484424, 0.62036035])
  2369. >>> s2 = pd.Series([0.12528585, 0.26962463, 0.51111198])
  2370. >>> s1.cov(s2)
  2371. -0.01685762652715874
  2372. """
  2373. this, other = self.align(other, join="inner", copy=False)
  2374. if len(this) == 0:
  2375. return np.nan
  2376. return nanops.nancov(
  2377. this.values, other.values, min_periods=min_periods, ddof=ddof
  2378. )
  2379. @doc(
  2380. klass="Series",
  2381. extra_params="",
  2382. other_klass="DataFrame",
  2383. examples=dedent(
  2384. """
  2385. Difference with previous row
  2386. >>> s = pd.Series([1, 1, 2, 3, 5, 8])
  2387. >>> s.diff()
  2388. 0 NaN
  2389. 1 0.0
  2390. 2 1.0
  2391. 3 1.0
  2392. 4 2.0
  2393. 5 3.0
  2394. dtype: float64
  2395. Difference with 3rd previous row
  2396. >>> s.diff(periods=3)
  2397. 0 NaN
  2398. 1 NaN
  2399. 2 NaN
  2400. 3 2.0
  2401. 4 4.0
  2402. 5 6.0
  2403. dtype: float64
  2404. Difference with following row
  2405. >>> s.diff(periods=-1)
  2406. 0 0.0
  2407. 1 -1.0
  2408. 2 -1.0
  2409. 3 -2.0
  2410. 4 -3.0
  2411. 5 NaN
  2412. dtype: float64
  2413. Overflow in input dtype
  2414. >>> s = pd.Series([1, 0], dtype=np.uint8)
  2415. >>> s.diff()
  2416. 0 NaN
  2417. 1 255.0
  2418. dtype: float64"""
  2419. ),
  2420. )
  2421. def diff(self, periods: int = 1) -> Series:
  2422. """
  2423. First discrete difference of element.
  2424. Calculates the difference of a {klass} element compared with another
  2425. element in the {klass} (default is element in previous row).
  2426. Parameters
  2427. ----------
  2428. periods : int, default 1
  2429. Periods to shift for calculating difference, accepts negative
  2430. values.
  2431. {extra_params}
  2432. Returns
  2433. -------
  2434. {klass}
  2435. First differences of the Series.
  2436. See Also
  2437. --------
  2438. {klass}.pct_change: Percent change over given number of periods.
  2439. {klass}.shift: Shift index by desired number of periods with an
  2440. optional time freq.
  2441. {other_klass}.diff: First discrete difference of object.
  2442. Notes
  2443. -----
  2444. For boolean dtypes, this uses :meth:`operator.xor` rather than
  2445. :meth:`operator.sub`.
  2446. The result is calculated according to current dtype in {klass},
  2447. however dtype of the result is always float64.
  2448. Examples
  2449. --------
  2450. {examples}
  2451. """
  2452. result = algorithms.diff(self._values, periods)
  2453. return self._constructor(result, index=self.index, copy=False).__finalize__(
  2454. self, method="diff"
  2455. )
  2456. def autocorr(self, lag: int = 1) -> float:
  2457. """
  2458. Compute the lag-N autocorrelation.
  2459. This method computes the Pearson correlation between
  2460. the Series and its shifted self.
  2461. Parameters
  2462. ----------
  2463. lag : int, default 1
  2464. Number of lags to apply before performing autocorrelation.
  2465. Returns
  2466. -------
  2467. float
  2468. The Pearson correlation between self and self.shift(lag).
  2469. See Also
  2470. --------
  2471. Series.corr : Compute the correlation between two Series.
  2472. Series.shift : Shift index by desired number of periods.
  2473. DataFrame.corr : Compute pairwise correlation of columns.
  2474. DataFrame.corrwith : Compute pairwise correlation between rows or
  2475. columns of two DataFrame objects.
  2476. Notes
  2477. -----
  2478. If the Pearson correlation is not well defined return 'NaN'.
  2479. Examples
  2480. --------
  2481. >>> s = pd.Series([0.25, 0.5, 0.2, -0.05])
  2482. >>> s.autocorr() # doctest: +ELLIPSIS
  2483. 0.10355...
  2484. >>> s.autocorr(lag=2) # doctest: +ELLIPSIS
  2485. -0.99999...
  2486. If the Pearson correlation is not well defined, then 'NaN' is returned.
  2487. >>> s = pd.Series([1, 0, 0, 0])
  2488. >>> s.autocorr()
  2489. nan
  2490. """
  2491. return self.corr(self.shift(lag))
  2492. def dot(self, other: AnyArrayLike) -> Series | np.ndarray:
  2493. """
  2494. Compute the dot product between the Series and the columns of other.
  2495. This method computes the dot product between the Series and another
  2496. one, or the Series and each columns of a DataFrame, or the Series and
  2497. each columns of an array.
  2498. It can also be called using `self @ other` in Python >= 3.5.
  2499. Parameters
  2500. ----------
  2501. other : Series, DataFrame or array-like
  2502. The other object to compute the dot product with its columns.
  2503. Returns
  2504. -------
  2505. scalar, Series or numpy.ndarray
  2506. Return the dot product of the Series and other if other is a
  2507. Series, the Series of the dot product of Series and each rows of
  2508. other if other is a DataFrame or a numpy.ndarray between the Series
  2509. and each columns of the numpy array.
  2510. See Also
  2511. --------
  2512. DataFrame.dot: Compute the matrix product with the DataFrame.
  2513. Series.mul: Multiplication of series and other, element-wise.
  2514. Notes
  2515. -----
  2516. The Series and other has to share the same index if other is a Series
  2517. or a DataFrame.
  2518. Examples
  2519. --------
  2520. >>> s = pd.Series([0, 1, 2, 3])
  2521. >>> other = pd.Series([-1, 2, -3, 4])
  2522. >>> s.dot(other)
  2523. 8
  2524. >>> s @ other
  2525. 8
  2526. >>> df = pd.DataFrame([[0, 1], [-2, 3], [4, -5], [6, 7]])
  2527. >>> s.dot(df)
  2528. 0 24
  2529. 1 14
  2530. dtype: int64
  2531. >>> arr = np.array([[0, 1], [-2, 3], [4, -5], [6, 7]])
  2532. >>> s.dot(arr)
  2533. array([24, 14])
  2534. """
  2535. if isinstance(other, (Series, ABCDataFrame)):
  2536. common = self.index.union(other.index)
  2537. if len(common) > len(self.index) or len(common) > len(other.index):
  2538. raise ValueError("matrices are not aligned")
  2539. left = self.reindex(index=common, copy=False)
  2540. right = other.reindex(index=common, copy=False)
  2541. lvals = left.values
  2542. rvals = right.values
  2543. else:
  2544. lvals = self.values
  2545. rvals = np.asarray(other)
  2546. if lvals.shape[0] != rvals.shape[0]:
  2547. raise Exception(
  2548. f"Dot product shape mismatch, {lvals.shape} vs {rvals.shape}"
  2549. )
  2550. if isinstance(other, ABCDataFrame):
  2551. return self._constructor(
  2552. np.dot(lvals, rvals), index=other.columns, copy=False
  2553. ).__finalize__(self, method="dot")
  2554. elif isinstance(other, Series):
  2555. return np.dot(lvals, rvals)
  2556. elif isinstance(rvals, np.ndarray):
  2557. return np.dot(lvals, rvals)
  2558. else: # pragma: no cover
  2559. raise TypeError(f"unsupported type: {type(other)}")
  2560. def __matmul__(self, other):
  2561. """
  2562. Matrix multiplication using binary `@` operator in Python>=3.5.
  2563. """
  2564. return self.dot(other)
  2565. def __rmatmul__(self, other):
  2566. """
  2567. Matrix multiplication using binary `@` operator in Python>=3.5.
  2568. """
  2569. return self.dot(np.transpose(other))
  2570. @doc(base.IndexOpsMixin.searchsorted, klass="Series")
  2571. # Signature of "searchsorted" incompatible with supertype "IndexOpsMixin"
  2572. def searchsorted( # type: ignore[override]
  2573. self,
  2574. value: NumpyValueArrayLike | ExtensionArray,
  2575. side: Literal["left", "right"] = "left",
  2576. sorter: NumpySorter = None,
  2577. ) -> npt.NDArray[np.intp] | np.intp:
  2578. return base.IndexOpsMixin.searchsorted(self, value, side=side, sorter=sorter)
  2579. # -------------------------------------------------------------------
  2580. # Combination
  2581. def _append(
  2582. self, to_append, ignore_index: bool = False, verify_integrity: bool = False
  2583. ):
  2584. from pandas.core.reshape.concat import concat
  2585. if isinstance(to_append, (list, tuple)):
  2586. to_concat = [self]
  2587. to_concat.extend(to_append)
  2588. else:
  2589. to_concat = [self, to_append]
  2590. if any(isinstance(x, (ABCDataFrame,)) for x in to_concat[1:]):
  2591. msg = "to_append should be a Series or list/tuple of Series, got DataFrame"
  2592. raise TypeError(msg)
  2593. return concat(
  2594. to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity
  2595. )
  2596. def _binop(self, other: Series, func, level=None, fill_value=None):
  2597. """
  2598. Perform generic binary operation with optional fill value.
  2599. Parameters
  2600. ----------
  2601. other : Series
  2602. func : binary operator
  2603. fill_value : float or object
  2604. Value to substitute for NA/null values. If both Series are NA in a
  2605. location, the result will be NA regardless of the passed fill value.
  2606. level : int or level name, default None
  2607. Broadcast across a level, matching Index values on the
  2608. passed MultiIndex level.
  2609. Returns
  2610. -------
  2611. Series
  2612. """
  2613. if not isinstance(other, Series):
  2614. raise AssertionError("Other operand must be Series")
  2615. this = self
  2616. if not self.index.equals(other.index):
  2617. this, other = self.align(other, level=level, join="outer", copy=False)
  2618. this_vals, other_vals = ops.fill_binop(this._values, other._values, fill_value)
  2619. with np.errstate(all="ignore"):
  2620. result = func(this_vals, other_vals)
  2621. name = ops.get_op_result_name(self, other)
  2622. return this._construct_result(result, name)
  2623. def _construct_result(
  2624. self, result: ArrayLike | tuple[ArrayLike, ArrayLike], name: Hashable
  2625. ) -> Series | tuple[Series, Series]:
  2626. """
  2627. Construct an appropriately-labelled Series from the result of an op.
  2628. Parameters
  2629. ----------
  2630. result : ndarray or ExtensionArray
  2631. name : Label
  2632. Returns
  2633. -------
  2634. Series
  2635. In the case of __divmod__ or __rdivmod__, a 2-tuple of Series.
  2636. """
  2637. if isinstance(result, tuple):
  2638. # produced by divmod or rdivmod
  2639. res1 = self._construct_result(result[0], name=name)
  2640. res2 = self._construct_result(result[1], name=name)
  2641. # GH#33427 assertions to keep mypy happy
  2642. assert isinstance(res1, Series)
  2643. assert isinstance(res2, Series)
  2644. return (res1, res2)
  2645. # TODO: result should always be ArrayLike, but this fails for some
  2646. # JSONArray tests
  2647. dtype = getattr(result, "dtype", None)
  2648. out = self._constructor(result, index=self.index, dtype=dtype)
  2649. out = out.__finalize__(self)
  2650. # Set the result's name after __finalize__ is called because __finalize__
  2651. # would set it back to self.name
  2652. out.name = name
  2653. return out
  2654. @doc(
  2655. _shared_docs["compare"],
  2656. """
  2657. Returns
  2658. -------
  2659. Series or DataFrame
  2660. If axis is 0 or 'index' the result will be a Series.
  2661. The resulting index will be a MultiIndex with 'self' and 'other'
  2662. stacked alternately at the inner level.
  2663. If axis is 1 or 'columns' the result will be a DataFrame.
  2664. It will have two columns namely 'self' and 'other'.
  2665. See Also
  2666. --------
  2667. DataFrame.compare : Compare with another DataFrame and show differences.
  2668. Notes
  2669. -----
  2670. Matching NaNs will not appear as a difference.
  2671. Examples
  2672. --------
  2673. >>> s1 = pd.Series(["a", "b", "c", "d", "e"])
  2674. >>> s2 = pd.Series(["a", "a", "c", "b", "e"])
  2675. Align the differences on columns
  2676. >>> s1.compare(s2)
  2677. self other
  2678. 1 b a
  2679. 3 d b
  2680. Stack the differences on indices
  2681. >>> s1.compare(s2, align_axis=0)
  2682. 1 self b
  2683. other a
  2684. 3 self d
  2685. other b
  2686. dtype: object
  2687. Keep all original rows
  2688. >>> s1.compare(s2, keep_shape=True)
  2689. self other
  2690. 0 NaN NaN
  2691. 1 b a
  2692. 2 NaN NaN
  2693. 3 d b
  2694. 4 NaN NaN
  2695. Keep all original rows and also all original values
  2696. >>> s1.compare(s2, keep_shape=True, keep_equal=True)
  2697. self other
  2698. 0 a a
  2699. 1 b a
  2700. 2 c c
  2701. 3 d b
  2702. 4 e e
  2703. """,
  2704. klass=_shared_doc_kwargs["klass"],
  2705. )
  2706. def compare(
  2707. self,
  2708. other: Series,
  2709. align_axis: Axis = 1,
  2710. keep_shape: bool = False,
  2711. keep_equal: bool = False,
  2712. result_names: Suffixes = ("self", "other"),
  2713. ) -> DataFrame | Series:
  2714. return super().compare(
  2715. other=other,
  2716. align_axis=align_axis,
  2717. keep_shape=keep_shape,
  2718. keep_equal=keep_equal,
  2719. result_names=result_names,
  2720. )
  2721. def combine(
  2722. self,
  2723. other: Series | Hashable,
  2724. func: Callable[[Hashable, Hashable], Hashable],
  2725. fill_value: Hashable = None,
  2726. ) -> Series:
  2727. """
  2728. Combine the Series with a Series or scalar according to `func`.
  2729. Combine the Series and `other` using `func` to perform elementwise
  2730. selection for combined Series.
  2731. `fill_value` is assumed when value is missing at some index
  2732. from one of the two objects being combined.
  2733. Parameters
  2734. ----------
  2735. other : Series or scalar
  2736. The value(s) to be combined with the `Series`.
  2737. func : function
  2738. Function that takes two scalars as inputs and returns an element.
  2739. fill_value : scalar, optional
  2740. The value to assume when an index is missing from
  2741. one Series or the other. The default specifies to use the
  2742. appropriate NaN value for the underlying dtype of the Series.
  2743. Returns
  2744. -------
  2745. Series
  2746. The result of combining the Series with the other object.
  2747. See Also
  2748. --------
  2749. Series.combine_first : Combine Series values, choosing the calling
  2750. Series' values first.
  2751. Examples
  2752. --------
  2753. Consider 2 Datasets ``s1`` and ``s2`` containing
  2754. highest clocked speeds of different birds.
  2755. >>> s1 = pd.Series({'falcon': 330.0, 'eagle': 160.0})
  2756. >>> s1
  2757. falcon 330.0
  2758. eagle 160.0
  2759. dtype: float64
  2760. >>> s2 = pd.Series({'falcon': 345.0, 'eagle': 200.0, 'duck': 30.0})
  2761. >>> s2
  2762. falcon 345.0
  2763. eagle 200.0
  2764. duck 30.0
  2765. dtype: float64
  2766. Now, to combine the two datasets and view the highest speeds
  2767. of the birds across the two datasets
  2768. >>> s1.combine(s2, max)
  2769. duck NaN
  2770. eagle 200.0
  2771. falcon 345.0
  2772. dtype: float64
  2773. In the previous example, the resulting value for duck is missing,
  2774. because the maximum of a NaN and a float is a NaN.
  2775. So, in the example, we set ``fill_value=0``,
  2776. so the maximum value returned will be the value from some dataset.
  2777. >>> s1.combine(s2, max, fill_value=0)
  2778. duck 30.0
  2779. eagle 200.0
  2780. falcon 345.0
  2781. dtype: float64
  2782. """
  2783. if fill_value is None:
  2784. fill_value = na_value_for_dtype(self.dtype, compat=False)
  2785. if isinstance(other, Series):
  2786. # If other is a Series, result is based on union of Series,
  2787. # so do this element by element
  2788. new_index = self.index.union(other.index)
  2789. new_name = ops.get_op_result_name(self, other)
  2790. new_values = np.empty(len(new_index), dtype=object)
  2791. for i, idx in enumerate(new_index):
  2792. lv = self.get(idx, fill_value)
  2793. rv = other.get(idx, fill_value)
  2794. with np.errstate(all="ignore"):
  2795. new_values[i] = func(lv, rv)
  2796. else:
  2797. # Assume that other is a scalar, so apply the function for
  2798. # each element in the Series
  2799. new_index = self.index
  2800. new_values = np.empty(len(new_index), dtype=object)
  2801. with np.errstate(all="ignore"):
  2802. new_values[:] = [func(lv, other) for lv in self._values]
  2803. new_name = self.name
  2804. # try_float=False is to match agg_series
  2805. npvalues = lib.maybe_convert_objects(new_values, try_float=False)
  2806. res_values = maybe_cast_pointwise_result(npvalues, self.dtype, same_dtype=False)
  2807. return self._constructor(res_values, index=new_index, name=new_name, copy=False)
  2808. def combine_first(self, other) -> Series:
  2809. """
  2810. Update null elements with value in the same location in 'other'.
  2811. Combine two Series objects by filling null values in one Series with
  2812. non-null values from the other Series. Result index will be the union
  2813. of the two indexes.
  2814. Parameters
  2815. ----------
  2816. other : Series
  2817. The value(s) to be used for filling null values.
  2818. Returns
  2819. -------
  2820. Series
  2821. The result of combining the provided Series with the other object.
  2822. See Also
  2823. --------
  2824. Series.combine : Perform element-wise operation on two Series
  2825. using a given function.
  2826. Examples
  2827. --------
  2828. >>> s1 = pd.Series([1, np.nan])
  2829. >>> s2 = pd.Series([3, 4, 5])
  2830. >>> s1.combine_first(s2)
  2831. 0 1.0
  2832. 1 4.0
  2833. 2 5.0
  2834. dtype: float64
  2835. Null values still persist if the location of that null value
  2836. does not exist in `other`
  2837. >>> s1 = pd.Series({'falcon': np.nan, 'eagle': 160.0})
  2838. >>> s2 = pd.Series({'eagle': 200.0, 'duck': 30.0})
  2839. >>> s1.combine_first(s2)
  2840. duck 30.0
  2841. eagle 160.0
  2842. falcon NaN
  2843. dtype: float64
  2844. """
  2845. new_index = self.index.union(other.index)
  2846. this = self.reindex(new_index, copy=False)
  2847. other = other.reindex(new_index, copy=False)
  2848. if this.dtype.kind == "M" and other.dtype.kind != "M":
  2849. other = to_datetime(other)
  2850. return this.where(notna(this), other)
  2851. def update(self, other: Series | Sequence | Mapping) -> None:
  2852. """
  2853. Modify Series in place using values from passed Series.
  2854. Uses non-NA values from passed Series to make updates. Aligns
  2855. on index.
  2856. Parameters
  2857. ----------
  2858. other : Series, or object coercible into Series
  2859. Examples
  2860. --------
  2861. >>> s = pd.Series([1, 2, 3])
  2862. >>> s.update(pd.Series([4, 5, 6]))
  2863. >>> s
  2864. 0 4
  2865. 1 5
  2866. 2 6
  2867. dtype: int64
  2868. >>> s = pd.Series(['a', 'b', 'c'])
  2869. >>> s.update(pd.Series(['d', 'e'], index=[0, 2]))
  2870. >>> s
  2871. 0 d
  2872. 1 b
  2873. 2 e
  2874. dtype: object
  2875. >>> s = pd.Series([1, 2, 3])
  2876. >>> s.update(pd.Series([4, 5, 6, 7, 8]))
  2877. >>> s
  2878. 0 4
  2879. 1 5
  2880. 2 6
  2881. dtype: int64
  2882. If ``other`` contains NaNs the corresponding values are not updated
  2883. in the original Series.
  2884. >>> s = pd.Series([1, 2, 3])
  2885. >>> s.update(pd.Series([4, np.nan, 6]))
  2886. >>> s
  2887. 0 4
  2888. 1 2
  2889. 2 6
  2890. dtype: int64
  2891. ``other`` can also be a non-Series object type
  2892. that is coercible into a Series
  2893. >>> s = pd.Series([1, 2, 3])
  2894. >>> s.update([4, np.nan, 6])
  2895. >>> s
  2896. 0 4
  2897. 1 2
  2898. 2 6
  2899. dtype: int64
  2900. >>> s = pd.Series([1, 2, 3])
  2901. >>> s.update({1: 9})
  2902. >>> s
  2903. 0 1
  2904. 1 9
  2905. 2 3
  2906. dtype: int64
  2907. """
  2908. if not isinstance(other, Series):
  2909. other = Series(other)
  2910. other = other.reindex_like(self)
  2911. mask = notna(other)
  2912. self._mgr = self._mgr.putmask(mask=mask, new=other)
  2913. self._maybe_update_cacher()
  2914. # ----------------------------------------------------------------------
  2915. # Reindexing, sorting
  2916. @overload
  2917. def sort_values(
  2918. self,
  2919. *,
  2920. axis: Axis = ...,
  2921. ascending: bool | int | Sequence[bool] | Sequence[int] = ...,
  2922. inplace: Literal[False] = ...,
  2923. kind: str = ...,
  2924. na_position: str = ...,
  2925. ignore_index: bool = ...,
  2926. key: ValueKeyFunc = ...,
  2927. ) -> Series:
  2928. ...
  2929. @overload
  2930. def sort_values(
  2931. self,
  2932. *,
  2933. axis: Axis = ...,
  2934. ascending: bool | int | Sequence[bool] | Sequence[int] = ...,
  2935. inplace: Literal[True],
  2936. kind: str = ...,
  2937. na_position: str = ...,
  2938. ignore_index: bool = ...,
  2939. key: ValueKeyFunc = ...,
  2940. ) -> None:
  2941. ...
  2942. def sort_values(
  2943. self,
  2944. *,
  2945. axis: Axis = 0,
  2946. ascending: bool | int | Sequence[bool] | Sequence[int] = True,
  2947. inplace: bool = False,
  2948. kind: str = "quicksort",
  2949. na_position: str = "last",
  2950. ignore_index: bool = False,
  2951. key: ValueKeyFunc = None,
  2952. ) -> Series | None:
  2953. """
  2954. Sort by the values.
  2955. Sort a Series in ascending or descending order by some
  2956. criterion.
  2957. Parameters
  2958. ----------
  2959. axis : {0 or 'index'}
  2960. Unused. Parameter needed for compatibility with DataFrame.
  2961. ascending : bool or list of bools, default True
  2962. If True, sort values in ascending order, otherwise descending.
  2963. inplace : bool, default False
  2964. If True, perform operation in-place.
  2965. kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
  2966. Choice of sorting algorithm. See also :func:`numpy.sort` for more
  2967. information. 'mergesort' and 'stable' are the only stable algorithms.
  2968. na_position : {'first' or 'last'}, default 'last'
  2969. Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
  2970. the end.
  2971. ignore_index : bool, default False
  2972. If True, the resulting axis will be labeled 0, 1, …, n - 1.
  2973. key : callable, optional
  2974. If not None, apply the key function to the series values
  2975. before sorting. This is similar to the `key` argument in the
  2976. builtin :meth:`sorted` function, with the notable difference that
  2977. this `key` function should be *vectorized*. It should expect a
  2978. ``Series`` and return an array-like.
  2979. .. versionadded:: 1.1.0
  2980. Returns
  2981. -------
  2982. Series or None
  2983. Series ordered by values or None if ``inplace=True``.
  2984. See Also
  2985. --------
  2986. Series.sort_index : Sort by the Series indices.
  2987. DataFrame.sort_values : Sort DataFrame by the values along either axis.
  2988. DataFrame.sort_index : Sort DataFrame by indices.
  2989. Examples
  2990. --------
  2991. >>> s = pd.Series([np.nan, 1, 3, 10, 5])
  2992. >>> s
  2993. 0 NaN
  2994. 1 1.0
  2995. 2 3.0
  2996. 3 10.0
  2997. 4 5.0
  2998. dtype: float64
  2999. Sort values ascending order (default behaviour)
  3000. >>> s.sort_values(ascending=True)
  3001. 1 1.0
  3002. 2 3.0
  3003. 4 5.0
  3004. 3 10.0
  3005. 0 NaN
  3006. dtype: float64
  3007. Sort values descending order
  3008. >>> s.sort_values(ascending=False)
  3009. 3 10.0
  3010. 4 5.0
  3011. 2 3.0
  3012. 1 1.0
  3013. 0 NaN
  3014. dtype: float64
  3015. Sort values putting NAs first
  3016. >>> s.sort_values(na_position='first')
  3017. 0 NaN
  3018. 1 1.0
  3019. 2 3.0
  3020. 4 5.0
  3021. 3 10.0
  3022. dtype: float64
  3023. Sort a series of strings
  3024. >>> s = pd.Series(['z', 'b', 'd', 'a', 'c'])
  3025. >>> s
  3026. 0 z
  3027. 1 b
  3028. 2 d
  3029. 3 a
  3030. 4 c
  3031. dtype: object
  3032. >>> s.sort_values()
  3033. 3 a
  3034. 1 b
  3035. 4 c
  3036. 2 d
  3037. 0 z
  3038. dtype: object
  3039. Sort using a key function. Your `key` function will be
  3040. given the ``Series`` of values and should return an array-like.
  3041. >>> s = pd.Series(['a', 'B', 'c', 'D', 'e'])
  3042. >>> s.sort_values()
  3043. 1 B
  3044. 3 D
  3045. 0 a
  3046. 2 c
  3047. 4 e
  3048. dtype: object
  3049. >>> s.sort_values(key=lambda x: x.str.lower())
  3050. 0 a
  3051. 1 B
  3052. 2 c
  3053. 3 D
  3054. 4 e
  3055. dtype: object
  3056. NumPy ufuncs work well here. For example, we can
  3057. sort by the ``sin`` of the value
  3058. >>> s = pd.Series([-4, -2, 0, 2, 4])
  3059. >>> s.sort_values(key=np.sin)
  3060. 1 -2
  3061. 4 4
  3062. 2 0
  3063. 0 -4
  3064. 3 2
  3065. dtype: int64
  3066. More complicated user-defined functions can be used,
  3067. as long as they expect a Series and return an array-like
  3068. >>> s.sort_values(key=lambda x: (np.tan(x.cumsum())))
  3069. 0 -4
  3070. 3 2
  3071. 4 4
  3072. 1 -2
  3073. 2 0
  3074. dtype: int64
  3075. """
  3076. inplace = validate_bool_kwarg(inplace, "inplace")
  3077. # Validate the axis parameter
  3078. self._get_axis_number(axis)
  3079. # GH 5856/5853
  3080. if inplace and self._is_cached:
  3081. raise ValueError(
  3082. "This Series is a view of some other array, to "
  3083. "sort in-place you must create a copy"
  3084. )
  3085. if is_list_like(ascending):
  3086. ascending = cast(Sequence[Union[bool, int]], ascending)
  3087. if len(ascending) != 1:
  3088. raise ValueError(
  3089. f"Length of ascending ({len(ascending)}) must be 1 for Series"
  3090. )
  3091. ascending = ascending[0]
  3092. ascending = validate_ascending(ascending)
  3093. if na_position not in ["first", "last"]:
  3094. raise ValueError(f"invalid na_position: {na_position}")
  3095. # GH 35922. Make sorting stable by leveraging nargsort
  3096. values_to_sort = ensure_key_mapped(self, key)._values if key else self._values
  3097. sorted_index = nargsort(values_to_sort, kind, bool(ascending), na_position)
  3098. if is_range_indexer(sorted_index, len(sorted_index)):
  3099. if inplace:
  3100. return self._update_inplace(self)
  3101. return self.copy(deep=None)
  3102. result = self._constructor(
  3103. self._values[sorted_index], index=self.index[sorted_index], copy=False
  3104. )
  3105. if ignore_index:
  3106. result.index = default_index(len(sorted_index))
  3107. if not inplace:
  3108. return result.__finalize__(self, method="sort_values")
  3109. self._update_inplace(result)
  3110. return None
  3111. @overload
  3112. def sort_index(
  3113. self,
  3114. *,
  3115. axis: Axis = ...,
  3116. level: IndexLabel = ...,
  3117. ascending: bool | Sequence[bool] = ...,
  3118. inplace: Literal[True],
  3119. kind: SortKind = ...,
  3120. na_position: NaPosition = ...,
  3121. sort_remaining: bool = ...,
  3122. ignore_index: bool = ...,
  3123. key: IndexKeyFunc = ...,
  3124. ) -> None:
  3125. ...
  3126. @overload
  3127. def sort_index(
  3128. self,
  3129. *,
  3130. axis: Axis = ...,
  3131. level: IndexLabel = ...,
  3132. ascending: bool | Sequence[bool] = ...,
  3133. inplace: Literal[False] = ...,
  3134. kind: SortKind = ...,
  3135. na_position: NaPosition = ...,
  3136. sort_remaining: bool = ...,
  3137. ignore_index: bool = ...,
  3138. key: IndexKeyFunc = ...,
  3139. ) -> Series:
  3140. ...
  3141. @overload
  3142. def sort_index(
  3143. self,
  3144. *,
  3145. axis: Axis = ...,
  3146. level: IndexLabel = ...,
  3147. ascending: bool | Sequence[bool] = ...,
  3148. inplace: bool = ...,
  3149. kind: SortKind = ...,
  3150. na_position: NaPosition = ...,
  3151. sort_remaining: bool = ...,
  3152. ignore_index: bool = ...,
  3153. key: IndexKeyFunc = ...,
  3154. ) -> Series | None:
  3155. ...
  3156. def sort_index(
  3157. self,
  3158. *,
  3159. axis: Axis = 0,
  3160. level: IndexLabel = None,
  3161. ascending: bool | Sequence[bool] = True,
  3162. inplace: bool = False,
  3163. kind: SortKind = "quicksort",
  3164. na_position: NaPosition = "last",
  3165. sort_remaining: bool = True,
  3166. ignore_index: bool = False,
  3167. key: IndexKeyFunc = None,
  3168. ) -> Series | None:
  3169. """
  3170. Sort Series by index labels.
  3171. Returns a new Series sorted by label if `inplace` argument is
  3172. ``False``, otherwise updates the original series and returns None.
  3173. Parameters
  3174. ----------
  3175. axis : {0 or 'index'}
  3176. Unused. Parameter needed for compatibility with DataFrame.
  3177. level : int, optional
  3178. If not None, sort on values in specified index level(s).
  3179. ascending : bool or list-like of bools, default True
  3180. Sort ascending vs. descending. When the index is a MultiIndex the
  3181. sort direction can be controlled for each level individually.
  3182. inplace : bool, default False
  3183. If True, perform operation in-place.
  3184. kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
  3185. Choice of sorting algorithm. See also :func:`numpy.sort` for more
  3186. information. 'mergesort' and 'stable' are the only stable algorithms. For
  3187. DataFrames, this option is only applied when sorting on a single
  3188. column or label.
  3189. na_position : {'first', 'last'}, default 'last'
  3190. If 'first' puts NaNs at the beginning, 'last' puts NaNs at the end.
  3191. Not implemented for MultiIndex.
  3192. sort_remaining : bool, default True
  3193. If True and sorting by level and index is multilevel, sort by other
  3194. levels too (in order) after sorting by specified level.
  3195. ignore_index : bool, default False
  3196. If True, the resulting axis will be labeled 0, 1, …, n - 1.
  3197. key : callable, optional
  3198. If not None, apply the key function to the index values
  3199. before sorting. This is similar to the `key` argument in the
  3200. builtin :meth:`sorted` function, with the notable difference that
  3201. this `key` function should be *vectorized*. It should expect an
  3202. ``Index`` and return an ``Index`` of the same shape.
  3203. .. versionadded:: 1.1.0
  3204. Returns
  3205. -------
  3206. Series or None
  3207. The original Series sorted by the labels or None if ``inplace=True``.
  3208. See Also
  3209. --------
  3210. DataFrame.sort_index: Sort DataFrame by the index.
  3211. DataFrame.sort_values: Sort DataFrame by the value.
  3212. Series.sort_values : Sort Series by the value.
  3213. Examples
  3214. --------
  3215. >>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, 4])
  3216. >>> s.sort_index()
  3217. 1 c
  3218. 2 b
  3219. 3 a
  3220. 4 d
  3221. dtype: object
  3222. Sort Descending
  3223. >>> s.sort_index(ascending=False)
  3224. 4 d
  3225. 3 a
  3226. 2 b
  3227. 1 c
  3228. dtype: object
  3229. By default NaNs are put at the end, but use `na_position` to place
  3230. them at the beginning
  3231. >>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, np.nan])
  3232. >>> s.sort_index(na_position='first')
  3233. NaN d
  3234. 1.0 c
  3235. 2.0 b
  3236. 3.0 a
  3237. dtype: object
  3238. Specify index level to sort
  3239. >>> arrays = [np.array(['qux', 'qux', 'foo', 'foo',
  3240. ... 'baz', 'baz', 'bar', 'bar']),
  3241. ... np.array(['two', 'one', 'two', 'one',
  3242. ... 'two', 'one', 'two', 'one'])]
  3243. >>> s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=arrays)
  3244. >>> s.sort_index(level=1)
  3245. bar one 8
  3246. baz one 6
  3247. foo one 4
  3248. qux one 2
  3249. bar two 7
  3250. baz two 5
  3251. foo two 3
  3252. qux two 1
  3253. dtype: int64
  3254. Does not sort by remaining levels when sorting by levels
  3255. >>> s.sort_index(level=1, sort_remaining=False)
  3256. qux one 2
  3257. foo one 4
  3258. baz one 6
  3259. bar one 8
  3260. qux two 1
  3261. foo two 3
  3262. baz two 5
  3263. bar two 7
  3264. dtype: int64
  3265. Apply a key function before sorting
  3266. >>> s = pd.Series([1, 2, 3, 4], index=['A', 'b', 'C', 'd'])
  3267. >>> s.sort_index(key=lambda x : x.str.lower())
  3268. A 1
  3269. b 2
  3270. C 3
  3271. d 4
  3272. dtype: int64
  3273. """
  3274. return super().sort_index(
  3275. axis=axis,
  3276. level=level,
  3277. ascending=ascending,
  3278. inplace=inplace,
  3279. kind=kind,
  3280. na_position=na_position,
  3281. sort_remaining=sort_remaining,
  3282. ignore_index=ignore_index,
  3283. key=key,
  3284. )
  3285. def argsort(
  3286. self,
  3287. axis: Axis = 0,
  3288. kind: SortKind = "quicksort",
  3289. order: None = None,
  3290. ) -> Series:
  3291. """
  3292. Return the integer indices that would sort the Series values.
  3293. Override ndarray.argsort. Argsorts the value, omitting NA/null values,
  3294. and places the result in the same locations as the non-NA values.
  3295. Parameters
  3296. ----------
  3297. axis : {0 or 'index'}
  3298. Unused. Parameter needed for compatibility with DataFrame.
  3299. kind : {'mergesort', 'quicksort', 'heapsort', 'stable'}, default 'quicksort'
  3300. Choice of sorting algorithm. See :func:`numpy.sort` for more
  3301. information. 'mergesort' and 'stable' are the only stable algorithms.
  3302. order : None
  3303. Has no effect but is accepted for compatibility with numpy.
  3304. Returns
  3305. -------
  3306. Series[np.intp]
  3307. Positions of values within the sort order with -1 indicating
  3308. nan values.
  3309. See Also
  3310. --------
  3311. numpy.ndarray.argsort : Returns the indices that would sort this array.
  3312. """
  3313. values = self._values
  3314. mask = isna(values)
  3315. if mask.any():
  3316. result = np.full(len(self), -1, dtype=np.intp)
  3317. notmask = ~mask
  3318. result[notmask] = np.argsort(values[notmask], kind=kind)
  3319. else:
  3320. result = np.argsort(values, kind=kind)
  3321. res = self._constructor(
  3322. result, index=self.index, name=self.name, dtype=np.intp, copy=False
  3323. )
  3324. return res.__finalize__(self, method="argsort")
  3325. def nlargest(
  3326. self, n: int = 5, keep: Literal["first", "last", "all"] = "first"
  3327. ) -> Series:
  3328. """
  3329. Return the largest `n` elements.
  3330. Parameters
  3331. ----------
  3332. n : int, default 5
  3333. Return this many descending sorted values.
  3334. keep : {'first', 'last', 'all'}, default 'first'
  3335. When there are duplicate values that cannot all fit in a
  3336. Series of `n` elements:
  3337. - ``first`` : return the first `n` occurrences in order
  3338. of appearance.
  3339. - ``last`` : return the last `n` occurrences in reverse
  3340. order of appearance.
  3341. - ``all`` : keep all occurrences. This can result in a Series of
  3342. size larger than `n`.
  3343. Returns
  3344. -------
  3345. Series
  3346. The `n` largest values in the Series, sorted in decreasing order.
  3347. See Also
  3348. --------
  3349. Series.nsmallest: Get the `n` smallest elements.
  3350. Series.sort_values: Sort Series by values.
  3351. Series.head: Return the first `n` rows.
  3352. Notes
  3353. -----
  3354. Faster than ``.sort_values(ascending=False).head(n)`` for small `n`
  3355. relative to the size of the ``Series`` object.
  3356. Examples
  3357. --------
  3358. >>> countries_population = {"Italy": 59000000, "France": 65000000,
  3359. ... "Malta": 434000, "Maldives": 434000,
  3360. ... "Brunei": 434000, "Iceland": 337000,
  3361. ... "Nauru": 11300, "Tuvalu": 11300,
  3362. ... "Anguilla": 11300, "Montserrat": 5200}
  3363. >>> s = pd.Series(countries_population)
  3364. >>> s
  3365. Italy 59000000
  3366. France 65000000
  3367. Malta 434000
  3368. Maldives 434000
  3369. Brunei 434000
  3370. Iceland 337000
  3371. Nauru 11300
  3372. Tuvalu 11300
  3373. Anguilla 11300
  3374. Montserrat 5200
  3375. dtype: int64
  3376. The `n` largest elements where ``n=5`` by default.
  3377. >>> s.nlargest()
  3378. France 65000000
  3379. Italy 59000000
  3380. Malta 434000
  3381. Maldives 434000
  3382. Brunei 434000
  3383. dtype: int64
  3384. The `n` largest elements where ``n=3``. Default `keep` value is 'first'
  3385. so Malta will be kept.
  3386. >>> s.nlargest(3)
  3387. France 65000000
  3388. Italy 59000000
  3389. Malta 434000
  3390. dtype: int64
  3391. The `n` largest elements where ``n=3`` and keeping the last duplicates.
  3392. Brunei will be kept since it is the last with value 434000 based on
  3393. the index order.
  3394. >>> s.nlargest(3, keep='last')
  3395. France 65000000
  3396. Italy 59000000
  3397. Brunei 434000
  3398. dtype: int64
  3399. The `n` largest elements where ``n=3`` with all duplicates kept. Note
  3400. that the returned Series has five elements due to the three duplicates.
  3401. >>> s.nlargest(3, keep='all')
  3402. France 65000000
  3403. Italy 59000000
  3404. Malta 434000
  3405. Maldives 434000
  3406. Brunei 434000
  3407. dtype: int64
  3408. """
  3409. return selectn.SelectNSeries(self, n=n, keep=keep).nlargest()
  3410. def nsmallest(self, n: int = 5, keep: str = "first") -> Series:
  3411. """
  3412. Return the smallest `n` elements.
  3413. Parameters
  3414. ----------
  3415. n : int, default 5
  3416. Return this many ascending sorted values.
  3417. keep : {'first', 'last', 'all'}, default 'first'
  3418. When there are duplicate values that cannot all fit in a
  3419. Series of `n` elements:
  3420. - ``first`` : return the first `n` occurrences in order
  3421. of appearance.
  3422. - ``last`` : return the last `n` occurrences in reverse
  3423. order of appearance.
  3424. - ``all`` : keep all occurrences. This can result in a Series of
  3425. size larger than `n`.
  3426. Returns
  3427. -------
  3428. Series
  3429. The `n` smallest values in the Series, sorted in increasing order.
  3430. See Also
  3431. --------
  3432. Series.nlargest: Get the `n` largest elements.
  3433. Series.sort_values: Sort Series by values.
  3434. Series.head: Return the first `n` rows.
  3435. Notes
  3436. -----
  3437. Faster than ``.sort_values().head(n)`` for small `n` relative to
  3438. the size of the ``Series`` object.
  3439. Examples
  3440. --------
  3441. >>> countries_population = {"Italy": 59000000, "France": 65000000,
  3442. ... "Brunei": 434000, "Malta": 434000,
  3443. ... "Maldives": 434000, "Iceland": 337000,
  3444. ... "Nauru": 11300, "Tuvalu": 11300,
  3445. ... "Anguilla": 11300, "Montserrat": 5200}
  3446. >>> s = pd.Series(countries_population)
  3447. >>> s
  3448. Italy 59000000
  3449. France 65000000
  3450. Brunei 434000
  3451. Malta 434000
  3452. Maldives 434000
  3453. Iceland 337000
  3454. Nauru 11300
  3455. Tuvalu 11300
  3456. Anguilla 11300
  3457. Montserrat 5200
  3458. dtype: int64
  3459. The `n` smallest elements where ``n=5`` by default.
  3460. >>> s.nsmallest()
  3461. Montserrat 5200
  3462. Nauru 11300
  3463. Tuvalu 11300
  3464. Anguilla 11300
  3465. Iceland 337000
  3466. dtype: int64
  3467. The `n` smallest elements where ``n=3``. Default `keep` value is
  3468. 'first' so Nauru and Tuvalu will be kept.
  3469. >>> s.nsmallest(3)
  3470. Montserrat 5200
  3471. Nauru 11300
  3472. Tuvalu 11300
  3473. dtype: int64
  3474. The `n` smallest elements where ``n=3`` and keeping the last
  3475. duplicates. Anguilla and Tuvalu will be kept since they are the last
  3476. with value 11300 based on the index order.
  3477. >>> s.nsmallest(3, keep='last')
  3478. Montserrat 5200
  3479. Anguilla 11300
  3480. Tuvalu 11300
  3481. dtype: int64
  3482. The `n` smallest elements where ``n=3`` with all duplicates kept. Note
  3483. that the returned Series has four elements due to the three duplicates.
  3484. >>> s.nsmallest(3, keep='all')
  3485. Montserrat 5200
  3486. Nauru 11300
  3487. Tuvalu 11300
  3488. Anguilla 11300
  3489. dtype: int64
  3490. """
  3491. return selectn.SelectNSeries(self, n=n, keep=keep).nsmallest()
  3492. @doc(
  3493. klass=_shared_doc_kwargs["klass"],
  3494. extra_params=dedent(
  3495. """copy : bool, default True
  3496. Whether to copy underlying data."""
  3497. ),
  3498. examples=dedent(
  3499. """\
  3500. Examples
  3501. --------
  3502. >>> s = pd.Series(
  3503. ... ["A", "B", "A", "C"],
  3504. ... index=[
  3505. ... ["Final exam", "Final exam", "Coursework", "Coursework"],
  3506. ... ["History", "Geography", "History", "Geography"],
  3507. ... ["January", "February", "March", "April"],
  3508. ... ],
  3509. ... )
  3510. >>> s
  3511. Final exam History January A
  3512. Geography February B
  3513. Coursework History March A
  3514. Geography April C
  3515. dtype: object
  3516. In the following example, we will swap the levels of the indices.
  3517. Here, we will swap the levels column-wise, but levels can be swapped row-wise
  3518. in a similar manner. Note that column-wise is the default behaviour.
  3519. By not supplying any arguments for i and j, we swap the last and second to
  3520. last indices.
  3521. >>> s.swaplevel()
  3522. Final exam January History A
  3523. February Geography B
  3524. Coursework March History A
  3525. April Geography C
  3526. dtype: object
  3527. By supplying one argument, we can choose which index to swap the last
  3528. index with. We can for example swap the first index with the last one as
  3529. follows.
  3530. >>> s.swaplevel(0)
  3531. January History Final exam A
  3532. February Geography Final exam B
  3533. March History Coursework A
  3534. April Geography Coursework C
  3535. dtype: object
  3536. We can also define explicitly which indices we want to swap by supplying values
  3537. for both i and j. Here, we for example swap the first and second indices.
  3538. >>> s.swaplevel(0, 1)
  3539. History Final exam January A
  3540. Geography Final exam February B
  3541. History Coursework March A
  3542. Geography Coursework April C
  3543. dtype: object"""
  3544. ),
  3545. )
  3546. def swaplevel(
  3547. self, i: Level = -2, j: Level = -1, copy: bool | None = None
  3548. ) -> Series:
  3549. """
  3550. Swap levels i and j in a :class:`MultiIndex`.
  3551. Default is to swap the two innermost levels of the index.
  3552. Parameters
  3553. ----------
  3554. i, j : int or str
  3555. Levels of the indices to be swapped. Can pass level name as string.
  3556. {extra_params}
  3557. Returns
  3558. -------
  3559. {klass}
  3560. {klass} with levels swapped in MultiIndex.
  3561. {examples}
  3562. """
  3563. assert isinstance(self.index, MultiIndex)
  3564. result = self.copy(deep=copy and not using_copy_on_write())
  3565. result.index = self.index.swaplevel(i, j)
  3566. return result
  3567. def reorder_levels(self, order: Sequence[Level]) -> Series:
  3568. """
  3569. Rearrange index levels using input order.
  3570. May not drop or duplicate levels.
  3571. Parameters
  3572. ----------
  3573. order : list of int representing new level order
  3574. Reference level by number or key.
  3575. Returns
  3576. -------
  3577. type of caller (new object)
  3578. """
  3579. if not isinstance(self.index, MultiIndex): # pragma: no cover
  3580. raise Exception("Can only reorder levels on a hierarchical axis.")
  3581. result = self.copy(deep=None)
  3582. assert isinstance(result.index, MultiIndex)
  3583. result.index = result.index.reorder_levels(order)
  3584. return result
  3585. def explode(self, ignore_index: bool = False) -> Series:
  3586. """
  3587. Transform each element of a list-like to a row.
  3588. Parameters
  3589. ----------
  3590. ignore_index : bool, default False
  3591. If True, the resulting index will be labeled 0, 1, …, n - 1.
  3592. .. versionadded:: 1.1.0
  3593. Returns
  3594. -------
  3595. Series
  3596. Exploded lists to rows; index will be duplicated for these rows.
  3597. See Also
  3598. --------
  3599. Series.str.split : Split string values on specified separator.
  3600. Series.unstack : Unstack, a.k.a. pivot, Series with MultiIndex
  3601. to produce DataFrame.
  3602. DataFrame.melt : Unpivot a DataFrame from wide format to long format.
  3603. DataFrame.explode : Explode a DataFrame from list-like
  3604. columns to long format.
  3605. Notes
  3606. -----
  3607. This routine will explode list-likes including lists, tuples, sets,
  3608. Series, and np.ndarray. The result dtype of the subset rows will
  3609. be object. Scalars will be returned unchanged, and empty list-likes will
  3610. result in a np.nan for that row. In addition, the ordering of elements in
  3611. the output will be non-deterministic when exploding sets.
  3612. Reference :ref:`the user guide <reshaping.explode>` for more examples.
  3613. Examples
  3614. --------
  3615. >>> s = pd.Series([[1, 2, 3], 'foo', [], [3, 4]])
  3616. >>> s
  3617. 0 [1, 2, 3]
  3618. 1 foo
  3619. 2 []
  3620. 3 [3, 4]
  3621. dtype: object
  3622. >>> s.explode()
  3623. 0 1
  3624. 0 2
  3625. 0 3
  3626. 1 foo
  3627. 2 NaN
  3628. 3 3
  3629. 3 4
  3630. dtype: object
  3631. """
  3632. if not len(self) or not is_object_dtype(self):
  3633. result = self.copy()
  3634. return result.reset_index(drop=True) if ignore_index else result
  3635. values, counts = reshape.explode(np.asarray(self._values))
  3636. if ignore_index:
  3637. index = default_index(len(values))
  3638. else:
  3639. index = self.index.repeat(counts)
  3640. return self._constructor(values, index=index, name=self.name, copy=False)
  3641. def unstack(self, level: IndexLabel = -1, fill_value: Hashable = None) -> DataFrame:
  3642. """
  3643. Unstack, also known as pivot, Series with MultiIndex to produce DataFrame.
  3644. Parameters
  3645. ----------
  3646. level : int, str, or list of these, default last level
  3647. Level(s) to unstack, can pass level name.
  3648. fill_value : scalar value, default None
  3649. Value to use when replacing NaN values.
  3650. Returns
  3651. -------
  3652. DataFrame
  3653. Unstacked Series.
  3654. Notes
  3655. -----
  3656. Reference :ref:`the user guide <reshaping.stacking>` for more examples.
  3657. Examples
  3658. --------
  3659. >>> s = pd.Series([1, 2, 3, 4],
  3660. ... index=pd.MultiIndex.from_product([['one', 'two'],
  3661. ... ['a', 'b']]))
  3662. >>> s
  3663. one a 1
  3664. b 2
  3665. two a 3
  3666. b 4
  3667. dtype: int64
  3668. >>> s.unstack(level=-1)
  3669. a b
  3670. one 1 2
  3671. two 3 4
  3672. >>> s.unstack(level=0)
  3673. one two
  3674. a 1 3
  3675. b 2 4
  3676. """
  3677. from pandas.core.reshape.reshape import unstack
  3678. return unstack(self, level, fill_value)
  3679. # ----------------------------------------------------------------------
  3680. # function application
  3681. def map(
  3682. self,
  3683. arg: Callable | Mapping | Series,
  3684. na_action: Literal["ignore"] | None = None,
  3685. ) -> Series:
  3686. """
  3687. Map values of Series according to an input mapping or function.
  3688. Used for substituting each value in a Series with another value,
  3689. that may be derived from a function, a ``dict`` or
  3690. a :class:`Series`.
  3691. Parameters
  3692. ----------
  3693. arg : function, collections.abc.Mapping subclass or Series
  3694. Mapping correspondence.
  3695. na_action : {None, 'ignore'}, default None
  3696. If 'ignore', propagate NaN values, without passing them to the
  3697. mapping correspondence.
  3698. Returns
  3699. -------
  3700. Series
  3701. Same index as caller.
  3702. See Also
  3703. --------
  3704. Series.apply : For applying more complex functions on a Series.
  3705. DataFrame.apply : Apply a function row-/column-wise.
  3706. DataFrame.applymap : Apply a function elementwise on a whole DataFrame.
  3707. Notes
  3708. -----
  3709. When ``arg`` is a dictionary, values in Series that are not in the
  3710. dictionary (as keys) are converted to ``NaN``. However, if the
  3711. dictionary is a ``dict`` subclass that defines ``__missing__`` (i.e.
  3712. provides a method for default values), then this default is used
  3713. rather than ``NaN``.
  3714. Examples
  3715. --------
  3716. >>> s = pd.Series(['cat', 'dog', np.nan, 'rabbit'])
  3717. >>> s
  3718. 0 cat
  3719. 1 dog
  3720. 2 NaN
  3721. 3 rabbit
  3722. dtype: object
  3723. ``map`` accepts a ``dict`` or a ``Series``. Values that are not found
  3724. in the ``dict`` are converted to ``NaN``, unless the dict has a default
  3725. value (e.g. ``defaultdict``):
  3726. >>> s.map({'cat': 'kitten', 'dog': 'puppy'})
  3727. 0 kitten
  3728. 1 puppy
  3729. 2 NaN
  3730. 3 NaN
  3731. dtype: object
  3732. It also accepts a function:
  3733. >>> s.map('I am a {}'.format)
  3734. 0 I am a cat
  3735. 1 I am a dog
  3736. 2 I am a nan
  3737. 3 I am a rabbit
  3738. dtype: object
  3739. To avoid applying the function to missing values (and keep them as
  3740. ``NaN``) ``na_action='ignore'`` can be used:
  3741. >>> s.map('I am a {}'.format, na_action='ignore')
  3742. 0 I am a cat
  3743. 1 I am a dog
  3744. 2 NaN
  3745. 3 I am a rabbit
  3746. dtype: object
  3747. """
  3748. new_values = self._map_values(arg, na_action=na_action)
  3749. return self._constructor(new_values, index=self.index, copy=False).__finalize__(
  3750. self, method="map"
  3751. )
  3752. def _gotitem(self, key, ndim, subset=None) -> Series:
  3753. """
  3754. Sub-classes to define. Return a sliced object.
  3755. Parameters
  3756. ----------
  3757. key : string / list of selections
  3758. ndim : {1, 2}
  3759. Requested ndim of result.
  3760. subset : object, default None
  3761. Subset to act on.
  3762. """
  3763. return self
  3764. _agg_see_also_doc = dedent(
  3765. """
  3766. See Also
  3767. --------
  3768. Series.apply : Invoke function on a Series.
  3769. Series.transform : Transform function producing a Series with like indexes.
  3770. """
  3771. )
  3772. _agg_examples_doc = dedent(
  3773. """
  3774. Examples
  3775. --------
  3776. >>> s = pd.Series([1, 2, 3, 4])
  3777. >>> s
  3778. 0 1
  3779. 1 2
  3780. 2 3
  3781. 3 4
  3782. dtype: int64
  3783. >>> s.agg('min')
  3784. 1
  3785. >>> s.agg(['min', 'max'])
  3786. min 1
  3787. max 4
  3788. dtype: int64
  3789. """
  3790. )
  3791. @doc(
  3792. _shared_docs["aggregate"],
  3793. klass=_shared_doc_kwargs["klass"],
  3794. axis=_shared_doc_kwargs["axis"],
  3795. see_also=_agg_see_also_doc,
  3796. examples=_agg_examples_doc,
  3797. )
  3798. def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
  3799. # Validate the axis parameter
  3800. self._get_axis_number(axis)
  3801. # if func is None, will switch to user-provided "named aggregation" kwargs
  3802. if func is None:
  3803. func = dict(kwargs.items())
  3804. op = SeriesApply(self, func, convert_dtype=False, args=args, kwargs=kwargs)
  3805. result = op.agg()
  3806. return result
  3807. agg = aggregate
  3808. # error: Signature of "any" incompatible with supertype "NDFrame" [override]
  3809. @overload # type: ignore[override]
  3810. def any(
  3811. self,
  3812. *,
  3813. axis: Axis = ...,
  3814. bool_only: bool | None = ...,
  3815. skipna: bool = ...,
  3816. level: None = ...,
  3817. **kwargs,
  3818. ) -> bool:
  3819. ...
  3820. @overload
  3821. def any(
  3822. self,
  3823. *,
  3824. axis: Axis = ...,
  3825. bool_only: bool | None = ...,
  3826. skipna: bool = ...,
  3827. level: Level,
  3828. **kwargs,
  3829. ) -> Series | bool:
  3830. ...
  3831. # error: Missing return statement
  3832. @doc(NDFrame.any, **_shared_doc_kwargs)
  3833. def any( # type: ignore[empty-body]
  3834. self,
  3835. axis: Axis = 0,
  3836. bool_only: bool | None = None,
  3837. skipna: bool = True,
  3838. level: Level | None = None,
  3839. **kwargs,
  3840. ) -> Series | bool:
  3841. ...
  3842. @doc(
  3843. _shared_docs["transform"],
  3844. klass=_shared_doc_kwargs["klass"],
  3845. axis=_shared_doc_kwargs["axis"],
  3846. )
  3847. def transform(
  3848. self, func: AggFuncType, axis: Axis = 0, *args, **kwargs
  3849. ) -> DataFrame | Series:
  3850. # Validate axis argument
  3851. self._get_axis_number(axis)
  3852. result = SeriesApply(
  3853. self, func=func, convert_dtype=True, args=args, kwargs=kwargs
  3854. ).transform()
  3855. return result
  3856. def apply(
  3857. self,
  3858. func: AggFuncType,
  3859. convert_dtype: bool = True,
  3860. args: tuple[Any, ...] = (),
  3861. **kwargs,
  3862. ) -> DataFrame | Series:
  3863. """
  3864. Invoke function on values of Series.
  3865. Can be ufunc (a NumPy function that applies to the entire Series)
  3866. or a Python function that only works on single values.
  3867. Parameters
  3868. ----------
  3869. func : function
  3870. Python function or NumPy ufunc to apply.
  3871. convert_dtype : bool, default True
  3872. Try to find better dtype for elementwise function results. If
  3873. False, leave as dtype=object. Note that the dtype is always
  3874. preserved for some extension array dtypes, such as Categorical.
  3875. args : tuple
  3876. Positional arguments passed to func after the series value.
  3877. **kwargs
  3878. Additional keyword arguments passed to func.
  3879. Returns
  3880. -------
  3881. Series or DataFrame
  3882. If func returns a Series object the result will be a DataFrame.
  3883. See Also
  3884. --------
  3885. Series.map: For element-wise operations.
  3886. Series.agg: Only perform aggregating type operations.
  3887. Series.transform: Only perform transforming type operations.
  3888. Notes
  3889. -----
  3890. Functions that mutate the passed object can produce unexpected
  3891. behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
  3892. for more details.
  3893. Examples
  3894. --------
  3895. Create a series with typical summer temperatures for each city.
  3896. >>> s = pd.Series([20, 21, 12],
  3897. ... index=['London', 'New York', 'Helsinki'])
  3898. >>> s
  3899. London 20
  3900. New York 21
  3901. Helsinki 12
  3902. dtype: int64
  3903. Square the values by defining a function and passing it as an
  3904. argument to ``apply()``.
  3905. >>> def square(x):
  3906. ... return x ** 2
  3907. >>> s.apply(square)
  3908. London 400
  3909. New York 441
  3910. Helsinki 144
  3911. dtype: int64
  3912. Square the values by passing an anonymous function as an
  3913. argument to ``apply()``.
  3914. >>> s.apply(lambda x: x ** 2)
  3915. London 400
  3916. New York 441
  3917. Helsinki 144
  3918. dtype: int64
  3919. Define a custom function that needs additional positional
  3920. arguments and pass these additional arguments using the
  3921. ``args`` keyword.
  3922. >>> def subtract_custom_value(x, custom_value):
  3923. ... return x - custom_value
  3924. >>> s.apply(subtract_custom_value, args=(5,))
  3925. London 15
  3926. New York 16
  3927. Helsinki 7
  3928. dtype: int64
  3929. Define a custom function that takes keyword arguments
  3930. and pass these arguments to ``apply``.
  3931. >>> def add_custom_values(x, **kwargs):
  3932. ... for month in kwargs:
  3933. ... x += kwargs[month]
  3934. ... return x
  3935. >>> s.apply(add_custom_values, june=30, july=20, august=25)
  3936. London 95
  3937. New York 96
  3938. Helsinki 87
  3939. dtype: int64
  3940. Use a function from the Numpy library.
  3941. >>> s.apply(np.log)
  3942. London 2.995732
  3943. New York 3.044522
  3944. Helsinki 2.484907
  3945. dtype: float64
  3946. """
  3947. return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
  3948. def _reduce(
  3949. self,
  3950. op,
  3951. name: str,
  3952. *,
  3953. axis: Axis = 0,
  3954. skipna: bool = True,
  3955. numeric_only: bool = False,
  3956. filter_type=None,
  3957. **kwds,
  3958. ):
  3959. """
  3960. Perform a reduction operation.
  3961. If we have an ndarray as a value, then simply perform the operation,
  3962. otherwise delegate to the object.
  3963. """
  3964. delegate = self._values
  3965. if axis is not None:
  3966. self._get_axis_number(axis)
  3967. if isinstance(delegate, ExtensionArray):
  3968. # dispatch to ExtensionArray interface
  3969. return delegate._reduce(name, skipna=skipna, **kwds)
  3970. else:
  3971. # dispatch to numpy arrays
  3972. if numeric_only and not is_numeric_dtype(self.dtype):
  3973. kwd_name = "numeric_only"
  3974. if name in ["any", "all"]:
  3975. kwd_name = "bool_only"
  3976. # GH#47500 - change to TypeError to match other methods
  3977. raise TypeError(
  3978. f"Series.{name} does not allow {kwd_name}={numeric_only} "
  3979. "with non-numeric dtypes."
  3980. )
  3981. with np.errstate(all="ignore"):
  3982. return op(delegate, skipna=skipna, **kwds)
  3983. def _reindex_indexer(
  3984. self,
  3985. new_index: Index | None,
  3986. indexer: npt.NDArray[np.intp] | None,
  3987. copy: bool | None,
  3988. ) -> Series:
  3989. # Note: new_index is None iff indexer is None
  3990. # if not None, indexer is np.intp
  3991. if indexer is None and (
  3992. new_index is None or new_index.names == self.index.names
  3993. ):
  3994. if using_copy_on_write():
  3995. return self.copy(deep=copy)
  3996. if copy or copy is None:
  3997. return self.copy(deep=copy)
  3998. return self
  3999. new_values = algorithms.take_nd(
  4000. self._values, indexer, allow_fill=True, fill_value=None
  4001. )
  4002. return self._constructor(new_values, index=new_index, copy=False)
  4003. def _needs_reindex_multi(self, axes, method, level) -> bool:
  4004. """
  4005. Check if we do need a multi reindex; this is for compat with
  4006. higher dims.
  4007. """
  4008. return False
  4009. # error: Cannot determine type of 'align'
  4010. @doc(
  4011. NDFrame.align, # type: ignore[has-type]
  4012. klass=_shared_doc_kwargs["klass"],
  4013. axes_single_arg=_shared_doc_kwargs["axes_single_arg"],
  4014. )
  4015. def align(
  4016. self,
  4017. other: Series,
  4018. join: AlignJoin = "outer",
  4019. axis: Axis | None = None,
  4020. level: Level = None,
  4021. copy: bool | None = None,
  4022. fill_value: Hashable = None,
  4023. method: FillnaOptions | None = None,
  4024. limit: int | None = None,
  4025. fill_axis: Axis = 0,
  4026. broadcast_axis: Axis | None = None,
  4027. ) -> Series:
  4028. return super().align(
  4029. other,
  4030. join=join,
  4031. axis=axis,
  4032. level=level,
  4033. copy=copy,
  4034. fill_value=fill_value,
  4035. method=method,
  4036. limit=limit,
  4037. fill_axis=fill_axis,
  4038. broadcast_axis=broadcast_axis,
  4039. )
  4040. @overload
  4041. def rename(
  4042. self,
  4043. index: Renamer | Hashable | None = ...,
  4044. *,
  4045. axis: Axis | None = ...,
  4046. copy: bool = ...,
  4047. inplace: Literal[True],
  4048. level: Level | None = ...,
  4049. errors: IgnoreRaise = ...,
  4050. ) -> None:
  4051. ...
  4052. @overload
  4053. def rename(
  4054. self,
  4055. index: Renamer | Hashable | None = ...,
  4056. *,
  4057. axis: Axis | None = ...,
  4058. copy: bool = ...,
  4059. inplace: Literal[False] = ...,
  4060. level: Level | None = ...,
  4061. errors: IgnoreRaise = ...,
  4062. ) -> Series:
  4063. ...
  4064. @overload
  4065. def rename(
  4066. self,
  4067. index: Renamer | Hashable | None = ...,
  4068. *,
  4069. axis: Axis | None = ...,
  4070. copy: bool = ...,
  4071. inplace: bool = ...,
  4072. level: Level | None = ...,
  4073. errors: IgnoreRaise = ...,
  4074. ) -> Series | None:
  4075. ...
  4076. def rename(
  4077. self,
  4078. index: Renamer | Hashable | None = None,
  4079. *,
  4080. axis: Axis | None = None,
  4081. copy: bool | None = None,
  4082. inplace: bool = False,
  4083. level: Level | None = None,
  4084. errors: IgnoreRaise = "ignore",
  4085. ) -> Series | None:
  4086. """
  4087. Alter Series index labels or name.
  4088. Function / dict values must be unique (1-to-1). Labels not contained in
  4089. a dict / Series will be left as-is. Extra labels listed don't throw an
  4090. error.
  4091. Alternatively, change ``Series.name`` with a scalar value.
  4092. See the :ref:`user guide <basics.rename>` for more.
  4093. Parameters
  4094. ----------
  4095. index : scalar, hashable sequence, dict-like or function optional
  4096. Functions or dict-like are transformations to apply to
  4097. the index.
  4098. Scalar or hashable sequence-like will alter the ``Series.name``
  4099. attribute.
  4100. axis : {0 or 'index'}
  4101. Unused. Parameter needed for compatibility with DataFrame.
  4102. copy : bool, default True
  4103. Also copy underlying data.
  4104. inplace : bool, default False
  4105. Whether to return a new Series. If True the value of copy is ignored.
  4106. level : int or level name, default None
  4107. In case of MultiIndex, only rename labels in the specified level.
  4108. errors : {'ignore', 'raise'}, default 'ignore'
  4109. If 'raise', raise `KeyError` when a `dict-like mapper` or
  4110. `index` contains labels that are not present in the index being transformed.
  4111. If 'ignore', existing keys will be renamed and extra keys will be ignored.
  4112. Returns
  4113. -------
  4114. Series or None
  4115. Series with index labels or name altered or None if ``inplace=True``.
  4116. See Also
  4117. --------
  4118. DataFrame.rename : Corresponding DataFrame method.
  4119. Series.rename_axis : Set the name of the axis.
  4120. Examples
  4121. --------
  4122. >>> s = pd.Series([1, 2, 3])
  4123. >>> s
  4124. 0 1
  4125. 1 2
  4126. 2 3
  4127. dtype: int64
  4128. >>> s.rename("my_name") # scalar, changes Series.name
  4129. 0 1
  4130. 1 2
  4131. 2 3
  4132. Name: my_name, dtype: int64
  4133. >>> s.rename(lambda x: x ** 2) # function, changes labels
  4134. 0 1
  4135. 1 2
  4136. 4 3
  4137. dtype: int64
  4138. >>> s.rename({1: 3, 2: 5}) # mapping, changes labels
  4139. 0 1
  4140. 3 2
  4141. 5 3
  4142. dtype: int64
  4143. """
  4144. if axis is not None:
  4145. # Make sure we raise if an invalid 'axis' is passed.
  4146. axis = self._get_axis_number(axis)
  4147. if callable(index) or is_dict_like(index):
  4148. # error: Argument 1 to "_rename" of "NDFrame" has incompatible
  4149. # type "Union[Union[Mapping[Any, Hashable], Callable[[Any],
  4150. # Hashable]], Hashable, None]"; expected "Union[Mapping[Any,
  4151. # Hashable], Callable[[Any], Hashable], None]"
  4152. return super()._rename(
  4153. index, # type: ignore[arg-type]
  4154. copy=copy,
  4155. inplace=inplace,
  4156. level=level,
  4157. errors=errors,
  4158. )
  4159. else:
  4160. return self._set_name(index, inplace=inplace, deep=copy)
  4161. @Appender(
  4162. """
  4163. Examples
  4164. --------
  4165. >>> s = pd.Series([1, 2, 3])
  4166. >>> s
  4167. 0 1
  4168. 1 2
  4169. 2 3
  4170. dtype: int64
  4171. >>> s.set_axis(['a', 'b', 'c'], axis=0)
  4172. a 1
  4173. b 2
  4174. c 3
  4175. dtype: int64
  4176. """
  4177. )
  4178. @Substitution(
  4179. **_shared_doc_kwargs,
  4180. extended_summary_sub="",
  4181. axis_description_sub="",
  4182. see_also_sub="",
  4183. )
  4184. @Appender(NDFrame.set_axis.__doc__)
  4185. def set_axis(
  4186. self,
  4187. labels,
  4188. *,
  4189. axis: Axis = 0,
  4190. copy: bool | None = None,
  4191. ) -> Series:
  4192. return super().set_axis(labels, axis=axis, copy=copy)
  4193. # error: Cannot determine type of 'reindex'
  4194. @doc(
  4195. NDFrame.reindex, # type: ignore[has-type]
  4196. klass=_shared_doc_kwargs["klass"],
  4197. optional_reindex=_shared_doc_kwargs["optional_reindex"],
  4198. )
  4199. def reindex( # type: ignore[override]
  4200. self,
  4201. index=None,
  4202. *,
  4203. axis: Axis | None = None,
  4204. method: str | None = None,
  4205. copy: bool | None = None,
  4206. level: Level | None = None,
  4207. fill_value: Scalar | None = None,
  4208. limit: int | None = None,
  4209. tolerance=None,
  4210. ) -> Series:
  4211. return super().reindex(
  4212. index=index,
  4213. method=method,
  4214. copy=copy,
  4215. level=level,
  4216. fill_value=fill_value,
  4217. limit=limit,
  4218. tolerance=tolerance,
  4219. )
  4220. @doc(NDFrame.rename_axis)
  4221. def rename_axis( # type: ignore[override]
  4222. self: Series,
  4223. mapper: IndexLabel | lib.NoDefault = lib.no_default,
  4224. *,
  4225. index=lib.no_default,
  4226. axis: Axis = 0,
  4227. copy: bool = True,
  4228. inplace: bool = False,
  4229. ) -> Series | None:
  4230. return super().rename_axis(
  4231. mapper=mapper,
  4232. index=index,
  4233. axis=axis,
  4234. copy=copy,
  4235. inplace=inplace,
  4236. )
  4237. @overload
  4238. def drop(
  4239. self,
  4240. labels: IndexLabel = ...,
  4241. *,
  4242. axis: Axis = ...,
  4243. index: IndexLabel = ...,
  4244. columns: IndexLabel = ...,
  4245. level: Level | None = ...,
  4246. inplace: Literal[True],
  4247. errors: IgnoreRaise = ...,
  4248. ) -> None:
  4249. ...
  4250. @overload
  4251. def drop(
  4252. self,
  4253. labels: IndexLabel = ...,
  4254. *,
  4255. axis: Axis = ...,
  4256. index: IndexLabel = ...,
  4257. columns: IndexLabel = ...,
  4258. level: Level | None = ...,
  4259. inplace: Literal[False] = ...,
  4260. errors: IgnoreRaise = ...,
  4261. ) -> Series:
  4262. ...
  4263. @overload
  4264. def drop(
  4265. self,
  4266. labels: IndexLabel = ...,
  4267. *,
  4268. axis: Axis = ...,
  4269. index: IndexLabel = ...,
  4270. columns: IndexLabel = ...,
  4271. level: Level | None = ...,
  4272. inplace: bool = ...,
  4273. errors: IgnoreRaise = ...,
  4274. ) -> Series | None:
  4275. ...
  4276. def drop(
  4277. self,
  4278. labels: IndexLabel = None,
  4279. *,
  4280. axis: Axis = 0,
  4281. index: IndexLabel = None,
  4282. columns: IndexLabel = None,
  4283. level: Level | None = None,
  4284. inplace: bool = False,
  4285. errors: IgnoreRaise = "raise",
  4286. ) -> Series | None:
  4287. """
  4288. Return Series with specified index labels removed.
  4289. Remove elements of a Series based on specifying the index labels.
  4290. When using a multi-index, labels on different levels can be removed
  4291. by specifying the level.
  4292. Parameters
  4293. ----------
  4294. labels : single label or list-like
  4295. Index labels to drop.
  4296. axis : {0 or 'index'}
  4297. Unused. Parameter needed for compatibility with DataFrame.
  4298. index : single label or list-like
  4299. Redundant for application on Series, but 'index' can be used instead
  4300. of 'labels'.
  4301. columns : single label or list-like
  4302. No change is made to the Series; use 'index' or 'labels' instead.
  4303. level : int or level name, optional
  4304. For MultiIndex, level for which the labels will be removed.
  4305. inplace : bool, default False
  4306. If True, do operation inplace and return None.
  4307. errors : {'ignore', 'raise'}, default 'raise'
  4308. If 'ignore', suppress error and only existing labels are dropped.
  4309. Returns
  4310. -------
  4311. Series or None
  4312. Series with specified index labels removed or None if ``inplace=True``.
  4313. Raises
  4314. ------
  4315. KeyError
  4316. If none of the labels are found in the index.
  4317. See Also
  4318. --------
  4319. Series.reindex : Return only specified index labels of Series.
  4320. Series.dropna : Return series without null values.
  4321. Series.drop_duplicates : Return Series with duplicate values removed.
  4322. DataFrame.drop : Drop specified labels from rows or columns.
  4323. Examples
  4324. --------
  4325. >>> s = pd.Series(data=np.arange(3), index=['A', 'B', 'C'])
  4326. >>> s
  4327. A 0
  4328. B 1
  4329. C 2
  4330. dtype: int64
  4331. Drop labels B en C
  4332. >>> s.drop(labels=['B', 'C'])
  4333. A 0
  4334. dtype: int64
  4335. Drop 2nd level label in MultiIndex Series
  4336. >>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
  4337. ... ['speed', 'weight', 'length']],
  4338. ... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
  4339. ... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
  4340. >>> s = pd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
  4341. ... index=midx)
  4342. >>> s
  4343. lama speed 45.0
  4344. weight 200.0
  4345. length 1.2
  4346. cow speed 30.0
  4347. weight 250.0
  4348. length 1.5
  4349. falcon speed 320.0
  4350. weight 1.0
  4351. length 0.3
  4352. dtype: float64
  4353. >>> s.drop(labels='weight', level=1)
  4354. lama speed 45.0
  4355. length 1.2
  4356. cow speed 30.0
  4357. length 1.5
  4358. falcon speed 320.0
  4359. length 0.3
  4360. dtype: float64
  4361. """
  4362. return super().drop(
  4363. labels=labels,
  4364. axis=axis,
  4365. index=index,
  4366. columns=columns,
  4367. level=level,
  4368. inplace=inplace,
  4369. errors=errors,
  4370. )
  4371. @overload
  4372. def fillna(
  4373. self,
  4374. value: Hashable | Mapping | Series | DataFrame = ...,
  4375. *,
  4376. method: FillnaOptions | None = ...,
  4377. axis: Axis | None = ...,
  4378. inplace: Literal[False] = ...,
  4379. limit: int | None = ...,
  4380. downcast: dict | None = ...,
  4381. ) -> Series:
  4382. ...
  4383. @overload
  4384. def fillna(
  4385. self,
  4386. value: Hashable | Mapping | Series | DataFrame = ...,
  4387. *,
  4388. method: FillnaOptions | None = ...,
  4389. axis: Axis | None = ...,
  4390. inplace: Literal[True],
  4391. limit: int | None = ...,
  4392. downcast: dict | None = ...,
  4393. ) -> None:
  4394. ...
  4395. @overload
  4396. def fillna(
  4397. self,
  4398. value: Hashable | Mapping | Series | DataFrame = ...,
  4399. *,
  4400. method: FillnaOptions | None = ...,
  4401. axis: Axis | None = ...,
  4402. inplace: bool = ...,
  4403. limit: int | None = ...,
  4404. downcast: dict | None = ...,
  4405. ) -> Series | None:
  4406. ...
  4407. @doc(NDFrame.fillna, **_shared_doc_kwargs)
  4408. def fillna(
  4409. self,
  4410. value: Hashable | Mapping | Series | DataFrame = None,
  4411. *,
  4412. method: FillnaOptions | None = None,
  4413. axis: Axis | None = None,
  4414. inplace: bool = False,
  4415. limit: int | None = None,
  4416. downcast: dict | None = None,
  4417. ) -> Series | None:
  4418. return super().fillna(
  4419. value=value,
  4420. method=method,
  4421. axis=axis,
  4422. inplace=inplace,
  4423. limit=limit,
  4424. downcast=downcast,
  4425. )
  4426. def pop(self, item: Hashable) -> Any:
  4427. """
  4428. Return item and drops from series. Raise KeyError if not found.
  4429. Parameters
  4430. ----------
  4431. item : label
  4432. Index of the element that needs to be removed.
  4433. Returns
  4434. -------
  4435. Value that is popped from series.
  4436. Examples
  4437. --------
  4438. >>> ser = pd.Series([1,2,3])
  4439. >>> ser.pop(0)
  4440. 1
  4441. >>> ser
  4442. 1 2
  4443. 2 3
  4444. dtype: int64
  4445. """
  4446. return super().pop(item=item)
  4447. @overload
  4448. def replace(
  4449. self,
  4450. to_replace=...,
  4451. value=...,
  4452. *,
  4453. inplace: Literal[False] = ...,
  4454. limit: int | None = ...,
  4455. regex: bool = ...,
  4456. method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ...,
  4457. ) -> Series:
  4458. ...
  4459. @overload
  4460. def replace(
  4461. self,
  4462. to_replace=...,
  4463. value=...,
  4464. *,
  4465. inplace: Literal[True],
  4466. limit: int | None = ...,
  4467. regex: bool = ...,
  4468. method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ...,
  4469. ) -> None:
  4470. ...
  4471. @doc(
  4472. NDFrame.replace,
  4473. klass=_shared_doc_kwargs["klass"],
  4474. inplace=_shared_doc_kwargs["inplace"],
  4475. replace_iloc=_shared_doc_kwargs["replace_iloc"],
  4476. )
  4477. def replace(
  4478. self,
  4479. to_replace=None,
  4480. value=lib.no_default,
  4481. *,
  4482. inplace: bool = False,
  4483. limit: int | None = None,
  4484. regex: bool = False,
  4485. method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = lib.no_default,
  4486. ) -> Series | None:
  4487. return super().replace(
  4488. to_replace=to_replace,
  4489. value=value,
  4490. inplace=inplace,
  4491. limit=limit,
  4492. regex=regex,
  4493. method=method,
  4494. )
  4495. @doc(INFO_DOCSTRING, **series_sub_kwargs)
  4496. def info(
  4497. self,
  4498. verbose: bool | None = None,
  4499. buf: IO[str] | None = None,
  4500. max_cols: int | None = None,
  4501. memory_usage: bool | str | None = None,
  4502. show_counts: bool = True,
  4503. ) -> None:
  4504. return SeriesInfo(self, memory_usage).render(
  4505. buf=buf,
  4506. max_cols=max_cols,
  4507. verbose=verbose,
  4508. show_counts=show_counts,
  4509. )
  4510. def _replace_single(self, to_replace, method: str, inplace: bool, limit):
  4511. """
  4512. Replaces values in a Series using the fill method specified when no
  4513. replacement value is given in the replace method
  4514. """
  4515. result = self if inplace else self.copy()
  4516. values = result._values
  4517. mask = missing.mask_missing(values, to_replace)
  4518. if isinstance(values, ExtensionArray):
  4519. # dispatch to the EA's _pad_mask_inplace method
  4520. values._fill_mask_inplace(method, limit, mask)
  4521. else:
  4522. fill_f = missing.get_fill_func(method)
  4523. fill_f(values, limit=limit, mask=mask)
  4524. if inplace:
  4525. return
  4526. return result
  4527. # error: Cannot determine type of 'shift'
  4528. @doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
  4529. def shift(
  4530. self, periods: int = 1, freq=None, axis: Axis = 0, fill_value: Hashable = None
  4531. ) -> Series:
  4532. return super().shift(
  4533. periods=periods, freq=freq, axis=axis, fill_value=fill_value
  4534. )
  4535. def memory_usage(self, index: bool = True, deep: bool = False) -> int:
  4536. """
  4537. Return the memory usage of the Series.
  4538. The memory usage can optionally include the contribution of
  4539. the index and of elements of `object` dtype.
  4540. Parameters
  4541. ----------
  4542. index : bool, default True
  4543. Specifies whether to include the memory usage of the Series index.
  4544. deep : bool, default False
  4545. If True, introspect the data deeply by interrogating
  4546. `object` dtypes for system-level memory consumption, and include
  4547. it in the returned value.
  4548. Returns
  4549. -------
  4550. int
  4551. Bytes of memory consumed.
  4552. See Also
  4553. --------
  4554. numpy.ndarray.nbytes : Total bytes consumed by the elements of the
  4555. array.
  4556. DataFrame.memory_usage : Bytes consumed by a DataFrame.
  4557. Examples
  4558. --------
  4559. >>> s = pd.Series(range(3))
  4560. >>> s.memory_usage()
  4561. 152
  4562. Not including the index gives the size of the rest of the data, which
  4563. is necessarily smaller:
  4564. >>> s.memory_usage(index=False)
  4565. 24
  4566. The memory footprint of `object` values is ignored by default:
  4567. >>> s = pd.Series(["a", "b"])
  4568. >>> s.values
  4569. array(['a', 'b'], dtype=object)
  4570. >>> s.memory_usage()
  4571. 144
  4572. >>> s.memory_usage(deep=True)
  4573. 244
  4574. """
  4575. v = self._memory_usage(deep=deep)
  4576. if index:
  4577. v += self.index.memory_usage(deep=deep)
  4578. return v
  4579. def isin(self, values) -> Series:
  4580. """
  4581. Whether elements in Series are contained in `values`.
  4582. Return a boolean Series showing whether each element in the Series
  4583. matches an element in the passed sequence of `values` exactly.
  4584. Parameters
  4585. ----------
  4586. values : set or list-like
  4587. The sequence of values to test. Passing in a single string will
  4588. raise a ``TypeError``. Instead, turn a single string into a
  4589. list of one element.
  4590. Returns
  4591. -------
  4592. Series
  4593. Series of booleans indicating if each element is in values.
  4594. Raises
  4595. ------
  4596. TypeError
  4597. * If `values` is a string
  4598. See Also
  4599. --------
  4600. DataFrame.isin : Equivalent method on DataFrame.
  4601. Examples
  4602. --------
  4603. >>> s = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama',
  4604. ... 'hippo'], name='animal')
  4605. >>> s.isin(['cow', 'lama'])
  4606. 0 True
  4607. 1 True
  4608. 2 True
  4609. 3 False
  4610. 4 True
  4611. 5 False
  4612. Name: animal, dtype: bool
  4613. To invert the boolean values, use the ``~`` operator:
  4614. >>> ~s.isin(['cow', 'lama'])
  4615. 0 False
  4616. 1 False
  4617. 2 False
  4618. 3 True
  4619. 4 False
  4620. 5 True
  4621. Name: animal, dtype: bool
  4622. Passing a single string as ``s.isin('lama')`` will raise an error. Use
  4623. a list of one element instead:
  4624. >>> s.isin(['lama'])
  4625. 0 True
  4626. 1 False
  4627. 2 True
  4628. 3 False
  4629. 4 True
  4630. 5 False
  4631. Name: animal, dtype: bool
  4632. Strings and integers are distinct and are therefore not comparable:
  4633. >>> pd.Series([1]).isin(['1'])
  4634. 0 False
  4635. dtype: bool
  4636. >>> pd.Series([1.1]).isin(['1.1'])
  4637. 0 False
  4638. dtype: bool
  4639. """
  4640. result = algorithms.isin(self._values, values)
  4641. return self._constructor(result, index=self.index, copy=False).__finalize__(
  4642. self, method="isin"
  4643. )
  4644. def between(
  4645. self,
  4646. left,
  4647. right,
  4648. inclusive: Literal["both", "neither", "left", "right"] = "both",
  4649. ) -> Series:
  4650. """
  4651. Return boolean Series equivalent to left <= series <= right.
  4652. This function returns a boolean vector containing `True` wherever the
  4653. corresponding Series element is between the boundary values `left` and
  4654. `right`. NA values are treated as `False`.
  4655. Parameters
  4656. ----------
  4657. left : scalar or list-like
  4658. Left boundary.
  4659. right : scalar or list-like
  4660. Right boundary.
  4661. inclusive : {"both", "neither", "left", "right"}
  4662. Include boundaries. Whether to set each bound as closed or open.
  4663. .. versionchanged:: 1.3.0
  4664. Returns
  4665. -------
  4666. Series
  4667. Series representing whether each element is between left and
  4668. right (inclusive).
  4669. See Also
  4670. --------
  4671. Series.gt : Greater than of series and other.
  4672. Series.lt : Less than of series and other.
  4673. Notes
  4674. -----
  4675. This function is equivalent to ``(left <= ser) & (ser <= right)``
  4676. Examples
  4677. --------
  4678. >>> s = pd.Series([2, 0, 4, 8, np.nan])
  4679. Boundary values are included by default:
  4680. >>> s.between(1, 4)
  4681. 0 True
  4682. 1 False
  4683. 2 True
  4684. 3 False
  4685. 4 False
  4686. dtype: bool
  4687. With `inclusive` set to ``"neither"`` boundary values are excluded:
  4688. >>> s.between(1, 4, inclusive="neither")
  4689. 0 True
  4690. 1 False
  4691. 2 False
  4692. 3 False
  4693. 4 False
  4694. dtype: bool
  4695. `left` and `right` can be any scalar value:
  4696. >>> s = pd.Series(['Alice', 'Bob', 'Carol', 'Eve'])
  4697. >>> s.between('Anna', 'Daniel')
  4698. 0 False
  4699. 1 True
  4700. 2 True
  4701. 3 False
  4702. dtype: bool
  4703. """
  4704. if inclusive == "both":
  4705. lmask = self >= left
  4706. rmask = self <= right
  4707. elif inclusive == "left":
  4708. lmask = self >= left
  4709. rmask = self < right
  4710. elif inclusive == "right":
  4711. lmask = self > left
  4712. rmask = self <= right
  4713. elif inclusive == "neither":
  4714. lmask = self > left
  4715. rmask = self < right
  4716. else:
  4717. raise ValueError(
  4718. "Inclusive has to be either string of 'both',"
  4719. "'left', 'right', or 'neither'."
  4720. )
  4721. return lmask & rmask
  4722. # ----------------------------------------------------------------------
  4723. # Convert to types that support pd.NA
  4724. def _convert_dtypes(
  4725. self,
  4726. infer_objects: bool = True,
  4727. convert_string: bool = True,
  4728. convert_integer: bool = True,
  4729. convert_boolean: bool = True,
  4730. convert_floating: bool = True,
  4731. dtype_backend: DtypeBackend = "numpy_nullable",
  4732. ) -> Series:
  4733. input_series = self
  4734. if infer_objects:
  4735. input_series = input_series.infer_objects()
  4736. if is_object_dtype(input_series):
  4737. input_series = input_series.copy(deep=None)
  4738. if convert_string or convert_integer or convert_boolean or convert_floating:
  4739. inferred_dtype = convert_dtypes(
  4740. input_series._values,
  4741. convert_string,
  4742. convert_integer,
  4743. convert_boolean,
  4744. convert_floating,
  4745. infer_objects,
  4746. dtype_backend,
  4747. )
  4748. result = input_series.astype(inferred_dtype)
  4749. else:
  4750. result = input_series.copy(deep=None)
  4751. return result
  4752. # error: Cannot determine type of 'isna'
  4753. # error: Return type "Series" of "isna" incompatible with return type "ndarray
  4754. # [Any, dtype[bool_]]" in supertype "IndexOpsMixin"
  4755. @doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
  4756. def isna(self) -> Series: # type: ignore[override]
  4757. return NDFrame.isna(self)
  4758. # error: Cannot determine type of 'isna'
  4759. @doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
  4760. def isnull(self) -> Series:
  4761. """
  4762. Series.isnull is an alias for Series.isna.
  4763. """
  4764. return super().isnull()
  4765. # error: Cannot determine type of 'notna'
  4766. @doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
  4767. def notna(self) -> Series:
  4768. return super().notna()
  4769. # error: Cannot determine type of 'notna'
  4770. @doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
  4771. def notnull(self) -> Series:
  4772. """
  4773. Series.notnull is an alias for Series.notna.
  4774. """
  4775. return super().notnull()
  4776. @overload
  4777. def dropna(
  4778. self,
  4779. *,
  4780. axis: Axis = ...,
  4781. inplace: Literal[False] = ...,
  4782. how: AnyAll | None = ...,
  4783. ignore_index: bool = ...,
  4784. ) -> Series:
  4785. ...
  4786. @overload
  4787. def dropna(
  4788. self,
  4789. *,
  4790. axis: Axis = ...,
  4791. inplace: Literal[True],
  4792. how: AnyAll | None = ...,
  4793. ignore_index: bool = ...,
  4794. ) -> None:
  4795. ...
  4796. def dropna(
  4797. self,
  4798. *,
  4799. axis: Axis = 0,
  4800. inplace: bool = False,
  4801. how: AnyAll | None = None,
  4802. ignore_index: bool = False,
  4803. ) -> Series | None:
  4804. """
  4805. Return a new Series with missing values removed.
  4806. See the :ref:`User Guide <missing_data>` for more on which values are
  4807. considered missing, and how to work with missing data.
  4808. Parameters
  4809. ----------
  4810. axis : {0 or 'index'}
  4811. Unused. Parameter needed for compatibility with DataFrame.
  4812. inplace : bool, default False
  4813. If True, do operation inplace and return None.
  4814. how : str, optional
  4815. Not in use. Kept for compatibility.
  4816. ignore_index : bool, default ``False``
  4817. If ``True``, the resulting axis will be labeled 0, 1, …, n - 1.
  4818. .. versionadded:: 2.0.0
  4819. Returns
  4820. -------
  4821. Series or None
  4822. Series with NA entries dropped from it or None if ``inplace=True``.
  4823. See Also
  4824. --------
  4825. Series.isna: Indicate missing values.
  4826. Series.notna : Indicate existing (non-missing) values.
  4827. Series.fillna : Replace missing values.
  4828. DataFrame.dropna : Drop rows or columns which contain NA values.
  4829. Index.dropna : Drop missing indices.
  4830. Examples
  4831. --------
  4832. >>> ser = pd.Series([1., 2., np.nan])
  4833. >>> ser
  4834. 0 1.0
  4835. 1 2.0
  4836. 2 NaN
  4837. dtype: float64
  4838. Drop NA values from a Series.
  4839. >>> ser.dropna()
  4840. 0 1.0
  4841. 1 2.0
  4842. dtype: float64
  4843. Empty strings are not considered NA values. ``None`` is considered an
  4844. NA value.
  4845. >>> ser = pd.Series([np.NaN, 2, pd.NaT, '', None, 'I stay'])
  4846. >>> ser
  4847. 0 NaN
  4848. 1 2
  4849. 2 NaT
  4850. 3
  4851. 4 None
  4852. 5 I stay
  4853. dtype: object
  4854. >>> ser.dropna()
  4855. 1 2
  4856. 3
  4857. 5 I stay
  4858. dtype: object
  4859. """
  4860. inplace = validate_bool_kwarg(inplace, "inplace")
  4861. ignore_index = validate_bool_kwarg(ignore_index, "ignore_index")
  4862. # Validate the axis parameter
  4863. self._get_axis_number(axis or 0)
  4864. if self._can_hold_na:
  4865. result = remove_na_arraylike(self)
  4866. else:
  4867. if not inplace:
  4868. result = self.copy(deep=None)
  4869. else:
  4870. result = self
  4871. if ignore_index:
  4872. result.index = default_index(len(result))
  4873. if inplace:
  4874. return self._update_inplace(result)
  4875. else:
  4876. return result
  4877. # ----------------------------------------------------------------------
  4878. # Time series-oriented methods
  4879. # error: Cannot determine type of 'asfreq'
  4880. @doc(NDFrame.asfreq, **_shared_doc_kwargs) # type: ignore[has-type]
  4881. def asfreq(
  4882. self,
  4883. freq: Frequency,
  4884. method: FillnaOptions | None = None,
  4885. how: str | None = None,
  4886. normalize: bool = False,
  4887. fill_value: Hashable = None,
  4888. ) -> Series:
  4889. return super().asfreq(
  4890. freq=freq,
  4891. method=method,
  4892. how=how,
  4893. normalize=normalize,
  4894. fill_value=fill_value,
  4895. )
  4896. # error: Cannot determine type of 'resample'
  4897. @doc(NDFrame.resample, **_shared_doc_kwargs) # type: ignore[has-type]
  4898. def resample(
  4899. self,
  4900. rule,
  4901. axis: Axis = 0,
  4902. closed: str | None = None,
  4903. label: str | None = None,
  4904. convention: str = "start",
  4905. kind: str | None = None,
  4906. on: Level = None,
  4907. level: Level = None,
  4908. origin: str | TimestampConvertibleTypes = "start_day",
  4909. offset: TimedeltaConvertibleTypes | None = None,
  4910. group_keys: bool = False,
  4911. ) -> Resampler:
  4912. return super().resample(
  4913. rule=rule,
  4914. axis=axis,
  4915. closed=closed,
  4916. label=label,
  4917. convention=convention,
  4918. kind=kind,
  4919. on=on,
  4920. level=level,
  4921. origin=origin,
  4922. offset=offset,
  4923. group_keys=group_keys,
  4924. )
  4925. def to_timestamp(
  4926. self,
  4927. freq=None,
  4928. how: Literal["s", "e", "start", "end"] = "start",
  4929. copy: bool | None = None,
  4930. ) -> Series:
  4931. """
  4932. Cast to DatetimeIndex of Timestamps, at *beginning* of period.
  4933. Parameters
  4934. ----------
  4935. freq : str, default frequency of PeriodIndex
  4936. Desired frequency.
  4937. how : {'s', 'e', 'start', 'end'}
  4938. Convention for converting period to timestamp; start of period
  4939. vs. end.
  4940. copy : bool, default True
  4941. Whether or not to return a copy.
  4942. Returns
  4943. -------
  4944. Series with DatetimeIndex
  4945. Examples
  4946. --------
  4947. >>> idx = pd.PeriodIndex(['2023', '2024', '2025'], freq='Y')
  4948. >>> s1 = pd.Series([1, 2, 3], index=idx)
  4949. >>> s1
  4950. 2023 1
  4951. 2024 2
  4952. 2025 3
  4953. Freq: A-DEC, dtype: int64
  4954. The resulting frequency of the Timestamps is `YearBegin`
  4955. >>> s1 = s1.to_timestamp()
  4956. >>> s1
  4957. 2023-01-01 1
  4958. 2024-01-01 2
  4959. 2025-01-01 3
  4960. Freq: AS-JAN, dtype: int64
  4961. Using `freq` which is the offset that the Timestamps will have
  4962. >>> s2 = pd.Series([1, 2, 3], index=idx)
  4963. >>> s2 = s2.to_timestamp(freq='M')
  4964. >>> s2
  4965. 2023-01-31 1
  4966. 2024-01-31 2
  4967. 2025-01-31 3
  4968. Freq: A-JAN, dtype: int64
  4969. """
  4970. if not isinstance(self.index, PeriodIndex):
  4971. raise TypeError(f"unsupported Type {type(self.index).__name__}")
  4972. new_obj = self.copy(deep=copy and not using_copy_on_write())
  4973. new_index = self.index.to_timestamp(freq=freq, how=how)
  4974. setattr(new_obj, "index", new_index)
  4975. return new_obj
  4976. def to_period(self, freq: str | None = None, copy: bool | None = None) -> Series:
  4977. """
  4978. Convert Series from DatetimeIndex to PeriodIndex.
  4979. Parameters
  4980. ----------
  4981. freq : str, default None
  4982. Frequency associated with the PeriodIndex.
  4983. copy : bool, default True
  4984. Whether or not to return a copy.
  4985. Returns
  4986. -------
  4987. Series
  4988. Series with index converted to PeriodIndex.
  4989. Examples
  4990. --------
  4991. >>> idx = pd.DatetimeIndex(['2023', '2024', '2025'])
  4992. >>> s = pd.Series([1, 2, 3], index=idx)
  4993. >>> s = s.to_period()
  4994. >>> s
  4995. 2023 1
  4996. 2024 2
  4997. 2025 3
  4998. Freq: A-DEC, dtype: int64
  4999. Viewing the index
  5000. >>> s.index
  5001. PeriodIndex(['2023', '2024', '2025'], dtype='period[A-DEC]')
  5002. """
  5003. if not isinstance(self.index, DatetimeIndex):
  5004. raise TypeError(f"unsupported Type {type(self.index).__name__}")
  5005. new_obj = self.copy(deep=copy and not using_copy_on_write())
  5006. new_index = self.index.to_period(freq=freq)
  5007. setattr(new_obj, "index", new_index)
  5008. return new_obj
  5009. @overload
  5010. def ffill(
  5011. self,
  5012. *,
  5013. axis: None | Axis = ...,
  5014. inplace: Literal[False] = ...,
  5015. limit: None | int = ...,
  5016. downcast: dict | None = ...,
  5017. ) -> Series:
  5018. ...
  5019. @overload
  5020. def ffill(
  5021. self,
  5022. *,
  5023. axis: None | Axis = ...,
  5024. inplace: Literal[True],
  5025. limit: None | int = ...,
  5026. downcast: dict | None = ...,
  5027. ) -> None:
  5028. ...
  5029. @overload
  5030. def ffill(
  5031. self,
  5032. *,
  5033. axis: None | Axis = ...,
  5034. inplace: bool = ...,
  5035. limit: None | int = ...,
  5036. downcast: dict | None = ...,
  5037. ) -> Series | None:
  5038. ...
  5039. def ffill(
  5040. self,
  5041. *,
  5042. axis: None | Axis = None,
  5043. inplace: bool = False,
  5044. limit: None | int = None,
  5045. downcast: dict | None = None,
  5046. ) -> Series | None:
  5047. return super().ffill(axis=axis, inplace=inplace, limit=limit, downcast=downcast)
  5048. @overload
  5049. def bfill(
  5050. self,
  5051. *,
  5052. axis: None | Axis = ...,
  5053. inplace: Literal[False] = ...,
  5054. limit: None | int = ...,
  5055. downcast: dict | None = ...,
  5056. ) -> Series:
  5057. ...
  5058. @overload
  5059. def bfill(
  5060. self,
  5061. *,
  5062. axis: None | Axis = ...,
  5063. inplace: Literal[True],
  5064. limit: None | int = ...,
  5065. downcast: dict | None = ...,
  5066. ) -> None:
  5067. ...
  5068. @overload
  5069. def bfill(
  5070. self,
  5071. *,
  5072. axis: None | Axis = ...,
  5073. inplace: bool = ...,
  5074. limit: None | int = ...,
  5075. downcast: dict | None = ...,
  5076. ) -> Series | None:
  5077. ...
  5078. def bfill(
  5079. self,
  5080. *,
  5081. axis: None | Axis = None,
  5082. inplace: bool = False,
  5083. limit: None | int = None,
  5084. downcast: dict | None = None,
  5085. ) -> Series | None:
  5086. return super().bfill(axis=axis, inplace=inplace, limit=limit, downcast=downcast)
  5087. def clip(
  5088. self: Series,
  5089. lower=None,
  5090. upper=None,
  5091. *,
  5092. axis: Axis | None = None,
  5093. inplace: bool = False,
  5094. **kwargs,
  5095. ) -> Series | None:
  5096. return super().clip(lower, upper, axis=axis, inplace=inplace, **kwargs)
  5097. def interpolate(
  5098. self: Series,
  5099. method: str = "linear",
  5100. *,
  5101. axis: Axis = 0,
  5102. limit: int | None = None,
  5103. inplace: bool = False,
  5104. limit_direction: str | None = None,
  5105. limit_area: str | None = None,
  5106. downcast: str | None = None,
  5107. **kwargs,
  5108. ) -> Series | None:
  5109. return super().interpolate(
  5110. method=method,
  5111. axis=axis,
  5112. limit=limit,
  5113. inplace=inplace,
  5114. limit_direction=limit_direction,
  5115. limit_area=limit_area,
  5116. downcast=downcast,
  5117. **kwargs,
  5118. )
  5119. @overload
  5120. def where(
  5121. self,
  5122. cond,
  5123. other=...,
  5124. *,
  5125. inplace: Literal[False] = ...,
  5126. axis: Axis | None = ...,
  5127. level: Level = ...,
  5128. ) -> Series:
  5129. ...
  5130. @overload
  5131. def where(
  5132. self,
  5133. cond,
  5134. other=...,
  5135. *,
  5136. inplace: Literal[True],
  5137. axis: Axis | None = ...,
  5138. level: Level = ...,
  5139. ) -> None:
  5140. ...
  5141. @overload
  5142. def where(
  5143. self,
  5144. cond,
  5145. other=...,
  5146. *,
  5147. inplace: bool = ...,
  5148. axis: Axis | None = ...,
  5149. level: Level = ...,
  5150. ) -> Series | None:
  5151. ...
  5152. def where(
  5153. self,
  5154. cond,
  5155. other=lib.no_default,
  5156. *,
  5157. inplace: bool = False,
  5158. axis: Axis | None = None,
  5159. level: Level = None,
  5160. ) -> Series | None:
  5161. return super().where(
  5162. cond,
  5163. other,
  5164. inplace=inplace,
  5165. axis=axis,
  5166. level=level,
  5167. )
  5168. @overload
  5169. def mask(
  5170. self,
  5171. cond,
  5172. other=...,
  5173. *,
  5174. inplace: Literal[False] = ...,
  5175. axis: Axis | None = ...,
  5176. level: Level = ...,
  5177. ) -> Series:
  5178. ...
  5179. @overload
  5180. def mask(
  5181. self,
  5182. cond,
  5183. other=...,
  5184. *,
  5185. inplace: Literal[True],
  5186. axis: Axis | None = ...,
  5187. level: Level = ...,
  5188. ) -> None:
  5189. ...
  5190. @overload
  5191. def mask(
  5192. self,
  5193. cond,
  5194. other=...,
  5195. *,
  5196. inplace: bool = ...,
  5197. axis: Axis | None = ...,
  5198. level: Level = ...,
  5199. ) -> Series | None:
  5200. ...
  5201. def mask(
  5202. self,
  5203. cond,
  5204. other=lib.no_default,
  5205. *,
  5206. inplace: bool = False,
  5207. axis: Axis | None = None,
  5208. level: Level = None,
  5209. ) -> Series | None:
  5210. return super().mask(
  5211. cond,
  5212. other,
  5213. inplace=inplace,
  5214. axis=axis,
  5215. level=level,
  5216. )
  5217. # ----------------------------------------------------------------------
  5218. # Add index
  5219. _AXIS_ORDERS: list[Literal["index", "columns"]] = ["index"]
  5220. _AXIS_LEN = len(_AXIS_ORDERS)
  5221. _info_axis_number: Literal[0] = 0
  5222. _info_axis_name: Literal["index"] = "index"
  5223. index = properties.AxisProperty(
  5224. axis=0, doc="The index (axis labels) of the Series."
  5225. )
  5226. # ----------------------------------------------------------------------
  5227. # Accessor Methods
  5228. # ----------------------------------------------------------------------
  5229. str = CachedAccessor("str", StringMethods)
  5230. dt = CachedAccessor("dt", CombinedDatetimelikeProperties)
  5231. cat = CachedAccessor("cat", CategoricalAccessor)
  5232. plot = CachedAccessor("plot", pandas.plotting.PlotAccessor)
  5233. sparse = CachedAccessor("sparse", SparseAccessor)
  5234. # ----------------------------------------------------------------------
  5235. # Add plotting methods to Series
  5236. hist = pandas.plotting.hist_series
  5237. # ----------------------------------------------------------------------
  5238. # Template-Based Arithmetic/Comparison Methods
  5239. def _cmp_method(self, other, op):
  5240. res_name = ops.get_op_result_name(self, other)
  5241. if isinstance(other, Series) and not self._indexed_same(other):
  5242. raise ValueError("Can only compare identically-labeled Series objects")
  5243. lvalues = self._values
  5244. rvalues = extract_array(other, extract_numpy=True, extract_range=True)
  5245. with np.errstate(all="ignore"):
  5246. res_values = ops.comparison_op(lvalues, rvalues, op)
  5247. return self._construct_result(res_values, name=res_name)
  5248. def _logical_method(self, other, op):
  5249. res_name = ops.get_op_result_name(self, other)
  5250. self, other = ops.align_method_SERIES(self, other, align_asobject=True)
  5251. lvalues = self._values
  5252. rvalues = extract_array(other, extract_numpy=True, extract_range=True)
  5253. res_values = ops.logical_op(lvalues, rvalues, op)
  5254. return self._construct_result(res_values, name=res_name)
  5255. def _arith_method(self, other, op):
  5256. self, other = ops.align_method_SERIES(self, other)
  5257. return base.IndexOpsMixin._arith_method(self, other, op)
  5258. Series._add_numeric_operations()
  5259. # Add arithmetic!
  5260. ops.add_flex_arithmetic_methods(Series)