Skip to content
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

Field length #98

Open
DCasoPTS opened this issue Oct 5, 2022 · 6 comments
Open

Field length #98

DCasoPTS opened this issue Oct 5, 2022 · 6 comments

Comments

@DCasoPTS
Copy link

DCasoPTS commented Oct 5, 2022

I'm having an issue with the length, I want to set it to the number I want but it is always fixed to the max length of the values inside the field.
I'm using a similar example to this:

import pandas as pd
import xport
import xport.v56

df = pd.DataFrame({
    'alpha': [10, 20, 30],
    'beta': ['x', 'y', 'z'],
})


...  # Analysis work ...

ds = xport.Dataset(df, name='DATA', label='Wonderful data')

# SAS variable names are limited to 8 characters.  As with Pandas
# dataframes, you must change the name on the dataset rather than
# the column directly.
ds = ds.rename(columns={k: k.upper()[:8] for k in ds})

# Other SAS metadata can be set on the columns themselves.
for k, v in ds.items():
    v.label = k.title()
    if v.dtype == 'object':
        v.format = '$CHAR20.'
    else:
        v.format = '10.2'

   v.length = 11

# Libraries can have multiple datasets.
library = xport.Library({'DATA': ds})

with open('example.xpt', 'wb') as f:
    xport.v56.dump(library, f)

v.length is doing nothing as I see in the result

@selik
Copy link
Owner

selik commented Oct 5, 2022

Are you trying to truncate your strings?

@selik
Copy link
Owner

selik commented Oct 5, 2022

If you want to truncate strings, do so in Python, first. If you're trying to set a longer FORMAT length, it's probably a bug.

@DCasoPTS
Copy link
Author

DCasoPTS commented Oct 5, 2022

Are you trying to truncate your strings?

Not at all, I just want to handle the field length so that if for example I put the values ["ABC", "ABCD", "AB"] instead of being length = 4 (max length in the values), I can put a higher one.
I know it's possible in the XPT files because I have seen examples where it happens (not created by Python).

@selik
Copy link
Owner

selik commented Oct 5, 2022

If it's not working, that's a bug in thexport module. It used to work. I'm not sure when I'll have time to look at it. Happy to have the help if you want to poke around.

@DCasoPTS
Copy link
Author

DCasoPTS commented Oct 5, 2022

If it's not working, that's a bug in thexport module. It used to work. I'm not sure when I'll have time to look at it. Happy to have the help if you want to poke around.

Going inside the package, I have found the issue, I was using v.length and it must be v.width
I think it could help for others to include this in the documentation.

Thank you for your help, and for your quickly response.

@selik
Copy link
Owner

selik commented Oct 5, 2022

I'd be happy to receive a pull request fixing the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants