@@ -400,6 +400,25 @@ def shape(self):
400
400
def dtype (self ):
401
401
return self .__values_container .dtype
402
402
403
+ def astype (self , dtype ) -> "SpeasyVariable" :
404
+ """Returns a SpeasyVariable with values converted to given dtype
405
+
406
+ Parameters
407
+ ----------
408
+ dtype : str or np.dtype or type
409
+ desired dtype
410
+
411
+ Returns
412
+ -------
413
+ SpeasyVariable
414
+ SpeasyVariable with values converted to given dtype
415
+ """
416
+ return SpeasyVariable (
417
+ axes = deepcopy (self .__axes ),
418
+ values = self .__values_container .astype (dtype ),
419
+ columns = deepcopy (self .__columns ),
420
+ )
421
+
403
422
@property
404
423
def name (self ) -> str :
405
424
"""SpeasyVariable name
@@ -700,14 +719,16 @@ def plot(self, *args, **kwargs):
700
719
values = self .__values_container , columns_names = self .columns , axes = self .axes
701
720
)
702
721
703
- def replace_fillval_by_nan (self , inplace = False ) -> "SpeasyVariable" :
704
- """Replaces fill values by NaN, non float values are automatically converted to float.
722
+ def replace_fillval_by_nan (self , inplace = False , convert_to_float = False ) -> "SpeasyVariable" :
723
+ """Replaces fill values by NaN, non float values are automatically converted to float if convert_to_float is True .
705
724
Fill value is taken from metadata field "FILLVAL"
706
725
707
726
Parameters
708
727
----------
709
728
inplace : bool, optional
710
729
Modifies source variable when true else modifies and returns a copy, by default False
730
+ convert_to_float : bool, optional
731
+ Automatically converts variable to float if true and needed, by default False.
711
732
712
733
Returns
713
734
-------
@@ -719,16 +740,25 @@ def replace_fillval_by_nan(self, inplace=False) -> "SpeasyVariable":
719
740
clamp_with_nan: replaces values outside valid range by NaN
720
741
sanitized: removes fill and invalid values
721
742
"""
743
+ # @TODO replace by a match case when Python 3.9 is EOL
722
744
if inplace :
723
745
res = self
746
+ if convert_to_float and not np .issubdtype (self .dtype , np .floating ):
747
+ res .__values_container = res .__values_container .astype (float )
724
748
else :
725
- res = deepcopy (self )
749
+ if convert_to_float and not np .issubdtype (self .dtype , np .floating ):
750
+ res = self .astype (float )
751
+ else :
752
+ res = deepcopy (self )
726
753
if (fill_value := self .fill_value ) is not None :
754
+ if convert_to_float and not np .issubdtype (res .dtype , np .floating ):
755
+ res .__values_container = res .__values_container .astype (float )
727
756
res [res == fill_value ] = np .nan
728
757
return res
729
758
730
- def clamp_with_nan (self , inplace = False , valid_min = None , valid_max = None ) -> "SpeasyVariable" :
731
- """Replaces values outside valid range by NaN, valid range is taken from metadata fields "VALIDMIN" and "VALIDMAX"
759
+ def clamp_with_nan (self , inplace = False , valid_min = None , valid_max = None , convert_to_float = False ) -> "SpeasyVariable" :
760
+ """Replaces values outside valid range by NaN, valid range is taken from metadata fields "VALIDMIN" and "VALIDMAX".
761
+ Automatically converts variable to float if convert_to_float is True and needed.
732
762
733
763
Parameters
734
764
----------
@@ -738,6 +768,8 @@ def clamp_with_nan(self, inplace=False, valid_min=None, valid_max=None) -> "Spea
738
768
Optional minimum valid value, takes metadata field "VALIDMIN" if not provided, by default None
739
769
valid_max : Float, optional
740
770
Optional maximum valid value, takes metadata field "VALIDMAX" if not provided, by default None
771
+ convert_to_float : bool, optional
772
+ Automatically converts variable to float if true and needed, by default False.
741
773
742
774
Returns
743
775
-------
@@ -749,10 +781,16 @@ def clamp_with_nan(self, inplace=False, valid_min=None, valid_max=None) -> "Spea
749
781
replace_fillval_by_nan: replaces fill values by NaN
750
782
sanitized: removes fill and invalid values
751
783
"""
784
+ # @TODO replace by a match case when Python 3.9 is EOL
752
785
if inplace :
753
786
res = self
787
+ if convert_to_float and not np .issubdtype (self .dtype , np .floating ):
788
+ res .__values_container = res .__values_container .astype (float )
754
789
else :
755
- res = deepcopy (self )
790
+ if convert_to_float and not np .issubdtype (self .dtype , np .floating ):
791
+ res = self .astype (float )
792
+ else :
793
+ res = deepcopy (self )
756
794
valid_min = valid_min or self .valid_range [0 ]
757
795
valid_max = valid_max or self .valid_range [1 ]
758
796
res [np .logical_or (res > valid_max , res < valid_min )] = np .nan
0 commit comments