Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
brivas
mdf_reader
Commits
e5abe117
Commit
e5abe117
authored
5 years ago
by
iregon
Browse files
Options
Download
Email Patches
Plain Diff
Object to numeric: translate right spaces to rigth zeros
parent
15fb45eb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
0 deletions
+5
-0
common/converters.py
common/converters.py
+5
-0
No files found.
common/converters.py
View file @
e5abe117
import
pandas
as
pd
import
numpy
as
np
from
..
import
properties
...
...
@@ -33,8 +34,12 @@ class df_converters():
self
.
numeric_scale
=
1.
if
self
.
dtype
in
properties
.
numpy_floats
else
1
self
.
numeric_offset
=
0.
if
self
.
dtype
in
properties
.
numpy_floats
else
0
def
object_to_numeric
(
self
,
data
,
scale
=
None
,
offset
=
None
):
scale
=
scale
if
scale
else
self
.
numeric_scale
offset
=
offset
if
offset
else
self
.
numeric_offset
# First do the appropriate managing of white spaces, to the right, they mean 0!
data
=
data
.
replace
(
r
'^\s*$'
,
np
.
nan
,
regex
=
True
)
data
=
data
.
str
.
replace
(
' '
,
'0'
)
# Convert to numeric, then scale (?!) and give it's actual int type
data
=
pd
.
to_numeric
(
data
,
errors
=
'coerce'
)
# astype fails on strings, to_numeric manages errors....!
data
=
offset
+
data
*
scale
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment