-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: sas, stata, style #36990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
TYP: sas, stata, style #36990
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf65d40
TYP: io.stata, io.sas
jbrockmendel 6a3d5c8
TYP: sas, stata, style
jbrockmendel f6e1323
Merge branch 'master' of https://github.com/pandas-dev/pandas into ty…
jbrockmendel 31ce75d
annotate, pep names
jbrockmendel 95ea5a6
Merge branch 'master' of https://github.com/pandas-dev/pandas into ty…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
from collections import abc | ||
from datetime import datetime, timedelta | ||
import struct | ||
from typing import IO, Any, Union | ||
|
||
import numpy as np | ||
|
||
|
@@ -62,12 +63,42 @@ def _convert_datetimes(sas_datetimes: pd.Series, unit: str) -> pd.Series: | |
raise ValueError("unit must be 'd' or 's'") | ||
|
||
|
||
class _subheader_pointer: | ||
pass | ||
class _SubheaderPointer: | ||
offset: int | ||
length: int | ||
compression: int | ||
ptype: int | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__(self, offset: int, length: int, compression: int, ptype: int): | ||
self.offset = offset | ||
self.length = length | ||
self.compression = compression | ||
self.ptype = ptype | ||
|
||
class _column: | ||
pass | ||
|
||
class _Column: | ||
col_id: int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be a dataclass to avoid this repetition |
||
name: Union[str, bytes] | ||
label: Union[str, bytes] | ||
format: Union[str, bytes] # TODO: i think allowing bytes is from py2 days | ||
ctype: bytes | ||
length: int | ||
|
||
def __init__( | ||
self, | ||
col_id: int, | ||
name: Union[str, bytes], | ||
label: Union[str, bytes], | ||
format: Union[str, bytes], | ||
ctype: bytes, | ||
length: int, | ||
): | ||
self.col_id = col_id | ||
self.name = name | ||
self.label = label | ||
self.format = format | ||
self.ctype = ctype | ||
self.length = length | ||
|
||
|
||
# SAS7BDAT represents a SAS data file in SAS7BDAT format. | ||
|
@@ -100,6 +131,8 @@ class SAS7BDATReader(ReaderBase, abc.Iterator): | |
bytes. | ||
""" | ||
|
||
_path_or_buf: IO[Any] | ||
|
||
def __init__( | ||
self, | ||
path_or_buf, | ||
|
@@ -121,7 +154,7 @@ def __init__( | |
self.convert_header_text = convert_header_text | ||
|
||
self.default_encoding = "latin-1" | ||
self.compression = "" | ||
self.compression = b"" | ||
self.column_names_strings = [] | ||
self.column_names = [] | ||
self.column_formats = [] | ||
|
@@ -137,10 +170,14 @@ def __init__( | |
self._current_row_on_page_index = 0 | ||
self._current_row_in_file_index = 0 | ||
|
||
self._path_or_buf = get_filepath_or_buffer(path_or_buf).filepath_or_buffer | ||
if isinstance(self._path_or_buf, str): | ||
self._path_or_buf = open(self._path_or_buf, "rb") | ||
self.handle = self._path_or_buf | ||
path_or_buf = get_filepath_or_buffer(path_or_buf).filepath_or_buffer | ||
if isinstance(path_or_buf, str): | ||
buf = open(path_or_buf, "rb") | ||
self.handle = buf | ||
else: | ||
buf = path_or_buf | ||
|
||
self._path_or_buf: IO[Any] = buf | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. define as a class attribute? |
||
|
||
try: | ||
self._get_properties() | ||
|
@@ -319,7 +356,7 @@ def _read_float(self, offset, width): | |
return struct.unpack(self.byte_order + fd, buf)[0] | ||
|
||
# Read a single signed integer of the given width (1, 2, 4 or 8). | ||
def _read_int(self, offset, width): | ||
def _read_int(self, offset: int, width: int) -> int: | ||
if width not in (1, 2, 4, 8): | ||
self.close() | ||
raise ValueError("invalid int width") | ||
|
@@ -328,7 +365,7 @@ def _read_int(self, offset, width): | |
iv = struct.unpack(self.byte_order + it, buf)[0] | ||
return iv | ||
|
||
def _read_bytes(self, offset, length): | ||
def _read_bytes(self, offset: int, length: int): | ||
if self._cached_page is None: | ||
self._path_or_buf.seek(offset) | ||
buf = self._path_or_buf.read(length) | ||
|
@@ -400,14 +437,14 @@ def _get_subheader_index(self, signature, compression, ptype): | |
if index is None: | ||
f1 = (compression == const.compressed_subheader_id) or (compression == 0) | ||
f2 = ptype == const.compressed_subheader_type | ||
if (self.compression != "") and f1 and f2: | ||
if (self.compression != b"") and f1 and f2: | ||
index = const.SASIndex.data_subheader_index | ||
else: | ||
self.close() | ||
raise ValueError("Unknown subheader signature") | ||
return index | ||
|
||
def _process_subheader_pointers(self, offset, subheader_pointer_index): | ||
def _process_subheader_pointers(self, offset: int, subheader_pointer_index: int): | ||
|
||
subheader_pointer_length = self._subheader_pointer_length | ||
total_offset = offset + subheader_pointer_length * subheader_pointer_index | ||
|
@@ -423,11 +460,9 @@ def _process_subheader_pointers(self, offset, subheader_pointer_index): | |
|
||
subheader_type = self._read_int(total_offset, 1) | ||
|
||
x = _subheader_pointer() | ||
x.offset = subheader_offset | ||
x.length = subheader_length | ||
x.compression = subheader_compression | ||
x.ptype = subheader_type | ||
x = _SubheaderPointer( | ||
subheader_offset, subheader_length, subheader_compression, subheader_type | ||
) | ||
|
||
return x | ||
|
||
|
@@ -519,7 +554,7 @@ def _process_columntext_subheader(self, offset, length): | |
self.column_names_strings.append(cname) | ||
|
||
if len(self.column_names_strings) == 1: | ||
compression_literal = "" | ||
compression_literal = b"" | ||
for cl in const.compression_literals: | ||
if cl in cname_raw: | ||
compression_literal = cl | ||
|
@@ -532,7 +567,7 @@ def _process_columntext_subheader(self, offset, length): | |
|
||
buf = self._read_bytes(offset1, self._lcp) | ||
compression_literal = buf.rstrip(b"\x00") | ||
if compression_literal == "": | ||
if compression_literal == b"": | ||
self._lcs = 0 | ||
offset1 = offset + 32 | ||
if self.U64: | ||
|
@@ -657,13 +692,14 @@ def _process_format_subheader(self, offset, length): | |
column_format = format_names[format_start : format_start + format_len] | ||
current_column_number = len(self.columns) | ||
|
||
col = _column() | ||
col.col_id = current_column_number | ||
col.name = self.column_names[current_column_number] | ||
col.label = column_label | ||
col.format = column_format | ||
col.ctype = self._column_types[current_column_number] | ||
col.length = self._column_data_lengths[current_column_number] | ||
col = _Column( | ||
current_column_number, | ||
self.column_names[current_column_number], | ||
column_label, | ||
column_format, | ||
self._column_types[current_column_number], | ||
self._column_data_lengths[current_column_number], | ||
) | ||
|
||
self.column_formats.append(column_format) | ||
self.columns.append(col) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this resolve if using an appropriate constructor like: https://docs.python.org/3.7/library/types.html (or the old type constructor)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i didnt know that
types.new_class
was a thing. id rather keep the more-idiomatic usage even if mypy doesnt like itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok