eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Example dataframe: testDf = pl.DataFrame({ "Date1": ["2024-04-01", "2024-04-06", "2024-04-07", "2024-04-10", "2024-04-11"], "Date2": ["2024-04-04", "2024-04-07", "2024-04-09", "2024-04-10", "2024-04-15"], "Date3": ["2024-04-07", "2024-04-08", "2024-04-10", "2024-05-15", "2024-04-21"], 'Value': [10, 15, -20, 5, 30] }).with_columns(pl.col('Date1').cast(pl.Date), pl.col('Date2').cast(pl.Date), pl.col('Date3').cast(pl.Date) ) shape: (5, 4) ┌────────────┬────────────┬────────────┬───────┐ │ Date1 ┆ Date2 ┆ Date3 ┆ Value │ │ --- ┆ --- ┆ --- ┆ --- │ │ date ┆ date ┆ date ┆ i64 │ ╞════════════╪════════════╪════════════╪═══════╡ │ 2024-04-01 ┆ 2024-04-04 ┆ 2024-04-07 ┆ 10 │ │ 2024-04-06 ┆ 2024-04-07 ┆ 2024-04-08 ┆ 15 │ │ 2024-04-07 ┆ 2024-04-09 ┆ 2024-04-10 ┆ -20 │ │ 2024-04-10 ┆ 2024-04-10 ┆ 2024-05-15 ┆ 5 │ │ 2024-04-11 ┆ 2024-04-15 ┆ 2024-04-21 ┆ 30 │ └────────────┴────────────┴────────────┴───────┘  What I would like to do is create a dataframe in which for each 'Date1' I would have a column of the cumulative sum of 'Value' where 'Date1' >= 'Date2' and 'Date1' <= 'Date3'. So when 'Date1' ='2024-04-10' the sum should read -15, since the first 2 rows 'Date3' <= '2024-04-10' and the last row has 'Date2' = '2024-04-15' >= '2024-04-10'. I tried this: testDf.group_by(pl.col('Date1'))\ .agg(pl.col('Value')\ .filter((pl.col('Date1') >= pl.col('Date2')) & (pl.col('Date1') <= pl.col('Date3')))\ .sum()) shape: (5, 2) ┌────────────┬───────┐ │ Date1 ┆ Value │ │ --- ┆ --- │ │ date ┆ i64 │ ╞════════════╪═══════╡ │ 2024-04-11 ┆ 0 │ │ 2024-04-06 ┆ 0 │ │ 2024-04-07 ┆ 0 │ │ 2024-04-10 ┆ 5 │ │ 2024-04-01 ┆ 0 │ └────────────┴───────┘  But my desired result is this: shape: (5, 2) ┌────────────┬─────┐ │ Date1 ┆ Sum │ │ --- ┆ --- │ │ date ┆ i64 │ ╞════════════╪═════╡ │ 2024-04-01 ┆ 0 │ │ 2024-04-06 ┆ 10 │ │ 2024-04-07 ┆ 25 │ │ 2024-04-10 ┆ -15 │ │ 2024-04-11 ┆ 5 │ └────────────┴─────┘ 

Answer: I'll need to think about it a bit more to understand whether a solution relying purely on polars' native expression API is possible. However, here is a preliminary solution relying on the discouraged pl.Expr.map_elements. ( testDf .with_columns( pl.col("Date1") .map_elements( lambda x: \ ( testDf .filter( pl.col("Date2") <= x, pl.col("Date3") >= x, ) .get_column("Value") .sum() ), return_dtype=pl.Int64 ) .alias("Sum") ) )  shape: (5, 5) ┌────────────┬────────────┬────────────┬───────┬─────┐ │ Date1 ┆ Date2 ┆ Date3 ┆ Value ┆ Sum │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ date ┆ date ┆ date ┆ i64 ┆ i64 │ ╞════════════╪════════════╪════════════╪═══════╪═════╡ │ 2024-04-01 ┆ 2024-04-04 ┆ 2024-04-07 ┆ 10 ┆ 0 │ │ 2024-04-06 ┆ 2024-04-07 ┆ 2024-04-08 ┆ 15 ┆ 10 │ │ 2024-04-07 ┆ 2024-04-09 ┆ 2024-04-10 ┆ -20 ┆ 25 │ │ 2024-04-10 ┆ 2024-04-10 ┆ 2024-05-15 ┆ 5 ┆ -15 │ │ 2024-04-11 ┆ 2024-04-15 ┆ 2024-04-21 ┆ 30 ┆ 5 │ └────────────┴────────────┴────────────┴───────┴─────┘
Question: This is my DataFrame: import pandas as pd df = pd.DataFrame( { 'a': [1, 1, 1, 2, 2, 2, 2, 2, -1, -1, 2, 2, 2], } )  Expected output: Changing column a:  a 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 -1 9 -1 10 2 11 2 12 2  The process is as follows: a) Finding streaks of 1s and 2s b) If a streak of 2 comes after a streak of 1, that streak of 2 changes to 1. For example: From rows 3 to 7 there is a streak of 2s. The streak before that is streak of 1s. So I want tot change rows 3 to 7 to 1 which is the expected output of this df. These are my attemps: # attempt 1 df['streak'] = df.a.ne(df.a.shift(1)).cumsum() # attempt 2 df.loc[(df.a.eq(2)) & (df.a.shift(1).eq(1)), 'a'] = 1 

Answer: You can use shift and ffill to access the previous group, then boolean indexing: # previous value s = df['a'].shift() # check if previous group is 1 m1 = s.where(df['a'].ne(s)).ffill().eq(1) # is value 2? m2 = df['a'].eq(2) df.loc[m1&m2, 'a'] = 1  Output:  a 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 -1 9 -1 10 2 11 2 12 2  Intermediates:  a s where ffill m1 m2 0 1 NaN NaN NaN False False 1 1 1.0 NaN NaN False False 2 1 1.0 NaN NaN False False 3 2 1.0 1.0 1.0 True True 4 2 2.0 NaN 1.0 True True 5 2 2.0 NaN 1.0 True True 6 2 2.0 NaN 1.0 True True 7 2 2.0 NaN 1.0 True True 8 -1 2.0 2.0 2.0 False False 9 -1 -1.0 NaN 2.0 False False 10 2 -1.0 -1.0 -1.0 False True 11 2 2.0 NaN -1.0 False True 12 2 2.0 NaN -1.0 False True
Question: Very new to python and coding in general! Trying to get a star pattern that looks like this: * ** *** **** ***** **** *** ** *  to work using a for loop, but I'm only allowed to use a single for loop, and embed an if/else statement inside to get it to work. If I could use 2 for loops I'd know how to do it but not sure why my current solution isn't working, as it only outputs the first 5 lines of stars and then stops. Any help appreciated :) My current codebase: print("Pattern: ") # Stores the * as a variable, not entirely necessary but negates having to type out "*" star = "*" for i in range(9): if i in range(5): print(star * (i + 1)) elif i in range(4, 0, -1): print(star * i) 

Answer: As others have mentioned in the comments, your range() structure is flawed. You can simplify the approach by using > and < in your if else like this:   print("Pattern: ") star = "*" for i in range(9): if i < 5: print(star * (i + 1)) else: print(star * (9 - i))
Question: I wanted to use contributions graph in my Django project, so after quick research I decided to use 'contributions-django' library. But when I tried to install it, I got stuck with this error::  FileNotFoundError: [Errno 2] No such file or directory: 'requirements.txt' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.  I am no pip expert, but I can see that requirements.txt file exists in this repository (altough it's empty). How to fix this? Can I install the libary manually? How? Thanks for help in advance. At the comment request, here is the command I used: first I activated virtual environment: my_venv\Scripts\activate  then I used this command to install the library: pip install contributions-django 

Answer: See https://github.com/vsoch/contributions-django/issues/3 . The bug was reported in 2020, still open. The last commit also was 4 years ago. The project seems to be abandoned. This command works, though: pip install git+https://github.com/vsoch/contributions-django.git  You need Git installed and git command present in the %PATH%.
Question: Is it possible to create a dataframe where one fragment is a view of another df and the remaining fragment is not a view? I am unable to create such a df, but I want to know if it is possible. If this is possible, can you give an example of such a dataframe?

Answer: I think you can make something like this when you construct the dataframe with copy=False parameter. Consider this: arr = np.array([[1, 2, 3], [4, 5, 6]]) df1 = pd.DataFrame(arr, columns=["a1", "b1", "c1"], copy=False) df2 = pd.DataFrame(arr, columns=["a2", "b2", "c2"], copy=False) df2["d"] = 999 print(df1) print(df2)  This prints:  a1 b1 c1 0 1 2 3 1 4 5 6 a2 b2 c2 d 0 1 2 3 999 1 4 5 6 999   Now when you do: df1.loc[0, :] = -1 print(df1) print(df2)  This prints:  a1 b1 c1 0 -1 -1 -1 1 4 5 6 a2 b2 c2 d 0 -1 -1 -1 999 1 4 5 6 999
Question: I have the following dataframe: data = {'a': {1: None, 2: 1, 3: 7, 4: 2, 5: 4}, 'b': {1: None, 2: 2, 3: 2, 4: 9, 5: 6}, 'c': {1: None, 2: 2.0, 3: None, 4: 7.0, 5: 4.0}} df = pd.DataFrame(data).rename_axis('day') a b c day 1 NaN NaN NaN 2 1.0 2.0 2.0 3 7.0 2.0 NaN 4 2.0 9.0 7.0 5 4.0 6.0 4.0  I want to get a new column ("std") with the rolling standard deviation of all column values. NaNs should be ignored. Let's say the number of rows to be included in the rolling window is 3 and min_periods (meaning the number of rows with at least one non-null value) is 2. This is the expected output:  a b c std day 1 NaN NaN NaN NaN 2 1.0 2.0 2.0 NaN 3 7.0 2.0 NaN 2.387467 4 2.0 9.0 7.0 3.116775 5 4.0 6.0 4.0 2.531939  The first std value (2.387467) is equal to np.std ([1,2,2,7,2], ddof=1). I tried both solutions proposed here but they don't work properly with my dataframe, probably because of NaNs.

Answer: You can use numpy.nanstd for working with missing values: #source https://stackoverflow.com/a/77704074/2901002 from numpy.lib.stride_tricks import sliding_window_view as swv N = 3 df.loc[df.index[N-1:], 'std'] = np.nanstd(swv(df.to_numpy(), N, axis=0), (1,2), ddof=1) print (df) a b c std day 1 NaN NaN NaN NaN 2 1.0 2.0 2.0 NaN 3 7.0 2.0 NaN 2.387467 4 2.0 9.0 7.0 3.116775 5 4.0 6.0 4.0 2.531939
Question: I have dataframe data = {'time': ['10:00', '10:01', '10:02', '10:02', '10:03','10:04', '10:06', '10:10', '10:15'], 'price': [100, 101, 101, 103, 101,101, 105, 106, 107], 'volume': [50, 60, 30, 80, 20,50, 10, 40, 40]}  I need to group by this df by every 5 minutes and price, sum up the volume df.groupby([df['time'].dt.floor('5T'), 'price']).agg({'volume' : 'sum'}).reset_index()  Then i need to find time when pandas groups them where after sum new volume i will get value more than 100. In this df i find 10:03 and after sum, value will be 60 + 30 + 20 = 110. In 10:04 sum will be 60 + 30 + 20 + 50 = 160 How can i do this using pandas?

Answer: It looks like you want the cumulated sum of the volume with groupby.cumsum: df['cum_volume'] = (df.groupby([df['time'].dt.floor('5min'), 'price']) ['volume'].cumsum() )  Updated df:  time price volume cum_volume 0 2024-04-06 10:00:00 100 50 50 1 2024-04-06 10:01:00 101 60 60 2 2024-04-06 10:02:00 101 30 90 3 2024-04-06 10:02:00 103 80 80 4 2024-04-06 10:03:00 101 20 110 5 2024-04-06 10:04:00 101 50 160 6 2024-04-06 10:06:00 105 10 10 7 2024-04-06 10:10:00 106 40 40 8 2024-04-06 10:15:00 107 40 40  You can then filter based on the value: out = df.query('cum_volume > 100')  Output:  time price volume cum_volume 4 2024-04-06 10:03:00 101 20 110 5 2024-04-06 10:04:00 101 50 160
Question: I tried to use driver.find_element, by "class_name" to find button and click on it for expanding rooms on - https://www.qantas.com/hotels/properties/18482?adults=2&checkIn=2024-04-16&checkOut=2024-04-17&children=0&infants=0#view-rooms  , but received error message raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".css-v84xw-NakedButton eml2css7"} (Session info: chrome=123.0.6312.106); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception  HTML code with button - <button data-testid="expand-offer-summary" aria-label="Expand offer details" type="button" class="css-v84xw-NakedButton eml2css7"><svg class="en7chz91 css-1osu69f-StyledSvg-Icon en7chz90" viewBox="0 0 24 24" title="expandMore" fill="currentcolor"><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"></path></svg></button>  Python code(I tried with explycity wait as wll - from selenium import webdriver from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC def rooms(): driver = webdriver.Chrome() driver.get("https://www.qantas.com/hotels/properties/18482?adults=2&checkIn=2024-04-16&checkOut=2024-04-17&children=0&infants=0#view-rooms") driver.implicitly_wait(5) content = driver.page_source soup = BeautifulSoup(content,'html.parser') Button = driver.find_element('class name',"css-v84xw-NakedButton eml2css7") Button.click() rooms() 

Answer: It should be like that. If there is only one class then use find_element(By.CLASS_NAME," #what is class name"). However, button has 2 different class so u need to use CSS_SELECTOR instead CLASS_NAME. Finally fyi "class name" doesn't work in find_elements as an argument. Button = driver.find_element(By.CSS_SELECTOR,".css-v84xw-NakedButton .eml2css7")  For more details you can take look on Selenium Doc.
Question: My Python version in my WSL Ubuntu is 3.10.12 and it's not upgrading through these commands even though 3.12.2 is released now. (My WSL Ubuntu version is 22.04)  sudo apt update sudo apt install python3 python3-pip  Will a particular distribution have control over how much you can upgrade a package? and is it recommended not to do it? is there a command I can run to check how much upgrade my particular distribution will allow for a package? say for python.

Answer: Linux distros normally have a Python version tied to the distro and used for various admin scripts. You should not expect that to follow the latest releases of Python. And don't try to force change it because you could break your OS. If you need newer Python versions for your work, install it in user space.
Question: I used to tackle this kind of thing reasonably quickly within DAX, but being new to pandas, I have been stuck for a while on this: I am trying to output a pivot table showing the % of visa sales per month (columns) and per city (rows). Here is the output I am looking for:  Jan Feb London 50.055991 56.435644 Paris 15.119760 67.170191  I've tried various pivot tables and group-by functions, which got me so close and yet so far from what I need. I'm just used to creating "measures" in Excel that I can add to the pivot table like a regular dimension or fact. Reproducible input: data = {'Month': {0: 'Jan', 1: 'Jan', 2: 'Jan', 3: 'Jan', 4: 'Feb', 5: 'Feb', 6: 'Feb', 7: 'Feb', 8: 'Feb'}, 'City': {0: 'Paris', 1: 'Paris', 2: 'London', 3: 'London', 4: 'Paris', 5: 'Paris', 6: 'London', 7: 'London', 8: 'Paris'}, 'Card': {0: 'Visa', 1: 'MasterCard', 2: 'Visa', 3: 'MasterCard', 4: 'Visa', 5: 'MasterCard', 6: 'Visa', 7: 'MasterCard', 8: 'Visa'}, ' Amount ': {0: ' $101 ', 1: ' $567 ', 2: ' $447 ', 3: ' $446 ', 4: ' $926 ', 5: ' $652 ', 6: ' $114 ', 7: ' $88 ', 8: ' $408 '}} df = pd.DataFrame.from_dict(data) df 

Answer: Using a pivot_table, pipe to compute the ratio, and unstack to reshape: df['Amount'] = pd.to_numeric(df['Amount'].str.strip(' $')) out = (df .pivot_table(index=['Month', 'City'], columns='Card', values='Amount', aggfunc='sum') .pipe(lambda x: x['Visa']/x.sum(axis=1)*100) .unstack('Month') )  Output: Month Feb Jan City London 56.435644 50.055991 Paris 67.170191 15.119760  To sort the months: from calendar import month_abbr months = {m:i for i, m in enumerate(month_abbr)} df['Amount'] = pd.to_numeric(df['Amount'].str.strip(' $')) out = (df .pivot_table(index=['Month', 'City'], columns='Card', values='Amount', aggfunc='sum') .pipe(lambda x: x['Visa']/x.sum(axis=1)*100) .unstack('Month').sort_index(axis=1, key=lambda x: x.map(months)) )  Output: Month Jan Feb City London 50.055991 56.435644 Paris 15.119760 67.170191
Question: I need to find documents that satisfy the entire list of passed parameters. I did it using raw query, but for my project specs, raw query can't be used, and I should use ORM. Raw query is: SELECT * FROM outbox_document WHERE document_summary like all(array['%par1%', '%par2%', '%par3%']);  It's works well, but I can't find an ORM analog for LIKE ALL. Please help!

Answer: You can use sqlalchemy.all_. OutBoxDocument.document_summary.like(all_(["%par1%", "%par2%", "%par3%"]))  This generates the following query SELECT outbox_document.id, outbox_document.document_summary FROM outbox_document WHERE outbox_document.document_summary LIKE ALL (%(param_1)s)  Complete code from sqlalchemy import create_engine, select, all_ from sqlalchemy.orm import Mapped, DeclarativeBase, mapped_column, Session engine = create_engine("postgresql+psycopg://some_connection_string") class Base(DeclarativeBase): pass class OutBoxDocument(Base): __tablename__ = "outbox_document" id: Mapped[int] = mapped_column(primary_key=True) document_summary: Mapped[str] Base.metadata.create_all(engine) with Session(engine) as session: session.add(OutBoxDocument(document_summary="par1 par2")) session.add(OutBoxDocument(document_summary="par1 par2 par3")) session.add(OutBoxDocument(document_summary="par1 par3")) session.commit() with Session(engine) as session: statement = select(OutBoxDocument).where(OutBoxDocument.document_summary.like(all_(["%par1%", "%par2%", "%par3%"]))) result = session.scalars(statement).all() print(result)  The flask sqlalchemy version would be db.session.scalars(statement).all()
Question: There is an icon in the program; by clicking on it, a window with information should appear on top of the program. How can this be implemented? import flet as ft def main(page: ft.page): page.title = "Тренировка интуиции" page.window_width = 400.00 page.window_height = 500.00 page.window_resizable = False def info(e): # info text pass page.add(ft.Row([ft.IconButton(ft.icons.HELP, on_click=info)], alignment=ft.alignment.center)) ft.app(target=main)  Please tell me, I’ve been studying recently

Answer: import flet as ft def main(page: ft.page): page.title = "Тренировка интуиции" page.window_width = 400.00 page.window_height = 500.00 page.window_resizable = False  Build info dialog with using AlertDialog. Add action to close your dialog. Content is the main content in pop-up.  def info(e): diaolog = ft.AlertDialog(title=ft.Text("Information"), content=ft.Text("Test Text"), actions=[ft.TextButton(text="Close", on_click=close_diaolog)], open=True, ) page.dialog = diaolog page.update()  Built close_diaolog to close dialog after click Close.  def close_diaolog(e): page.dialog.open = False page.update() page.add(ft.Row([ft.IconButton(ft.icons.HELP, on_click=info)], alignment=ft.alignment.center)) ft.app(target=main)
Question: Came across some pattern similar to this for a leetcode problem. Basically, both functions sums a list a recursively using a nonlocal value. The unassigned value only updates res once it seems. def assigned_sum(l: list[int]): res = 0 def recurse(i: int): nonlocal res if i >= len(l): return 0 assigned = recurse(i+1) res += assigned return l[i] recurse(-1) return res def rvalue_sum(l: list[int]): res = 0 def recurse(i: int): nonlocal res if i >= len(l): return 0 res += recurse(i+1) return l[i] recurse(-1) return res test = [1,2,3,4,5] f"expected={sum(test)}, lvalue={assigned_sum(test)}, rvalue={rvalue_sum(test)}"  When thrown into colab, I get 'expected=15, lvalue=15, rvalue=1'

Answer: The difference can be seen more clearly between these two variants: (a): res = res + recurse(i+1)  and (b): res = recurse(i+1) + res  For your test run, (a) will return 1, while (b) will return the intended 15. The difference is caused by the moment when the value of res is taken: before the recursive call or after. If it is taken before the recursive call, it will always be 0. This is because the assignment to res only happens when unwinding out of recursion, not when entering it. So by consequence, all the reading of res that happens at each recursion level, happens before any assignment to res. Then when all the assignments happen to res, they each overwrite the earlier results: 0 + 5, then 0 + 4, then 0 + 3, ... until 0 + 1 which is the final value that is assigned to res. In the correct version, the value of res is read when unwinding out of recursion, so that means we read the value of res after it has been updated by the recursive call, and so we assign to res: 1 + 0, 2 + 1, 3 + 3, 4 + 6, and finally 5 + 10.
Question: What are the steps to post a Pandas dataframe in a Stack Overflow question? I found: How to make good reproducible pandas examples. I followed the instructions and used pd.read_clipboard, but I still had to spend a significant amount of time formatting the table to make it look correct. I also found: How to display a pandas dataframe on a Stack Overflow question body. I tried to copy the dataframe from Jupyter and paste it into a Blockquote. As mentioned, I also ran pd.read_clipboard('\s\s+') in Jupyter to copy it to the clipboard and then pasted it into a Blockquote. I also tried creating a table and pasting the values in the table. All of these methods required that I tweak the formatting to make it look properly formatted. An example dataframe: df = pd.DataFrame( [['Captain', 'Crunch', 72], ['Trix', 'Rabbit', 36], ['Count', 'Chocula', 41], ['Tony', 'Tiger', 54], ['Buzz', 'Bee', 28], ['Toucan', 'Sam', 38]], columns=['first_name', 'last_name', 'age']) 

Answer: .to_markdown() The easiest method I found was to use print(df.to_markdown()). This will convert the data into mkd format which can be interpreted by SO. For example with your dataframe, the output is:     first_name last_name age     0 Captain Crunch 72   1 Trix 36 Rabbit   2 Count Chocula 41   3 Tony 54 Tiger   4 Buzz 28 Bee   5 Toucan Sam 38    Note you might need to install tabulate module. .to_dict() Another option is to use df.head().to_dict('list'), but it might not be the best one for large datasets (will work for minimum reproducible examples though) {'first_name': ['Captain', 'Trix', 'Count', 'Tony', 'Buzz'], 'last_name': ['Crunch', 36, 'Chocula', 54, 28], 'age': [72, 'Rabbit', 41, 'Tiger', 'Bee']}  Anyone can use this by passing it through pd.DataFrame()
Question: I have a polars Dataframe with two columns: a string column containing datetimes and an integer column containing UTC offsets (for example -4 for EDT). Essentially the Dataframe looks like this: >>> data shape: (2, 2) ┌─────────────────────┬──────────┐ │ Datetime ┆ Timezone │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═════════════════════╪══════════╡ │ 2022-01-01 12:52:23 ┆ -4 │ │ 2023-03-31 04:22:59 ┆ -5 │ └─────────────────────┴──────────┘  Now I want to convert this column either to UTC or a timezone-aware datetime column. I looked into the pl.Expr.str.to_datetime function which accepts the time_zone argument. Unfortunately this argument can only be passed as a string and not as a pl.Expr. In other words, I can convert all columns to the same specified timezone, but I cannot dynamically use timezone based on the value of another column. What I would like in the end is something like the following (note that the Datetime column is now of datetime type and the timezone offset has been added dynamically (4 hours for the first row and 5 for the second). >>> data shape: (2, 2) ┌─────────────────────┬──────────┐ │ Datetime ┆ Timezone │ │ --- ┆ --- │ │ datetime ┆ i64 │ ╞═════════════════════╪══════════╡ │ 2022-01-01 16:52:23 ┆ -4 │ │ 2023-03-31 09:22:59 ┆ -5 │ └─────────────────────┴──────────┘  Is there a way to do this without going to map_elements or iter_rows based approaches?

Answer: If you're ok with keeping things in UTC, you can use df = pl.DataFrame( {"Datetime": ["2022-01-01 12:52:23", "2023-03-31 04:22:59"], "Timezone": [-4, -5]} ) df.with_columns( pl.col("Datetime") .str.to_datetime("%Y-%m-%d %H:%M:%S", time_zone="UTC") .dt.offset_by(pl.format("{}h", -pl.col("Timezone"))) .alias("dt_conv") )  shape: (2, 3) ┌─────────────────────┬──────────┬─────────────────────────┐ │ Datetime ┆ Timezone ┆ dt_conv │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ datetime[μs, UTC] │ ╞═════════════════════╪══════════╪═════════════════════════╡ │ 2022-01-01 12:52:23 ┆ -4 ┆ 2022-01-01 16:52:23 UTC │ │ 2023-03-31 04:22:59 ┆ -5 ┆ 2023-03-31 09:22:59 UTC │ └─────────────────────┴──────────┴─────────────────────────┘