pandas-path
pandas-path copied to clipboard
`.path.parent` returns a string rather than a `Path`
The attribute .parent
returns a string rather than a Path, like in pathlib
.
from pathlib import Path
import pandas as pd
from pandas_path import path as _
df = pd.DataFrame({"file_path": [Path("tata.txt"), Path("toto/titi.txt")]})
df["parent"] = df.file_path.path.parent
df.to_dict()
#> { 'file_path': {0: WindowsPath('tata.txt'), 1: WindowsPath('toto/titi.txt')},
#> 'parent': {0: '.', 1: 'toto'}}
The current workaround is to apply a Path
after getting the parent.
...
df["parent"] = df.file_path.path.parent.apply(Path)
df.to_dict()
#> { 'file_path': {0: WindowsPath('tata.txt'), 1: WindowsPath('toto/titi.txt')},
#> 'parent': {0: WindowsPath('.'), 1: WindowsPath('toto')}}
Let's note that the related test is currently expecting a string so this might be updated: https://github.com/drivendataorg/pandas-path/blob/8852e1c79113340127ba8c7c29f57c363128e3b2/pandas_path/tests.py#L56